首页 文章

As.vector不会将data.frame子集转换为vector

提问于
浏览
2

下面我提供了一些我一直在研究的代码片段 . 我已成功读取字符串作为表格 . 我希望使用中位数()函数存在我的表的一些子集 . 根据我所做的研究和我自己的经验,median()没有为data.frame定义行为,也没有定义data.frame的子集 . 因此,为了使我的问题适合某些定义的行为,我试图将我想要的子集投射到矢量中 . 但是,即使使用as.vector投射我想要的子集,我仍然有一个data.frame . 当我尝试调用中位数时,我得到“参数不是数字或逻辑:返回NA” .

我自己玩了很多,并试图在这里和其他地方找到信息 . 作为一个说明,我已尝试在此线程R-friendly way to convert R data.frame column to a vector?上的重复解决方案中列出的方法,并取得了与现在相同的结果 . 我不太关心我是如何做到这一点的;随意提出其他方法 .

感谢您的时间 .

for(i in 1:length(text_array)){
    temp= read.table(textConnection(text_array[i]), sep="\t",row.names=NULL, header= FALSE,  fill=TRUE) 
    value=""
    #we are now going to process temp and add it
    cur_DS=coll_data_sets[i]
    #median is the value that we are going to insert into the result array.
    #currently the logic behind it is not implemented.
    #the value will be the median of state1 divided by the median of state2.
    t_states=vector(length=ncol(temp))
    for(j in 1:ncol(temp)){
        t_states[j]=toString(temp[2,j])

    }
    t_states=(unique(t_states))
    #this logic is current is set to reject data from more than one state.
    # It will also reject anything that appears to lack state data.
    if(length(t_states) !=  2){
        value=NA
    }else{
        s1_expr=as.vector(x=(temp[3, temp[2,]==t_states[1]]))
        s2_expr=as.vector(x=temp[3, temp[2,]==t_states[2]])
        print(class(s1_expr))
    #   med1= (median(s1_expr))
    #   med2= (median(s2_expr))
    #   if(is.na(med1[1]) || is.na(med2[1])){
    #       value=-1
        }#else{
    #       value=med1[1]/med2[1]
    #       print(value)
    #   }

    }
[1] "data.frame"
[1] "data.frame"
[1] "data.frame"

以下是'temp'的示例值:

V1        V2          V3          V4
1 GSM506899 GSM506900   GSM506901   GSM506902
2 wild type wild type Zbtb20 null Zbtb20 null
3      99.3     98.24        66.2      102.42
4      55.8     20.11        22.9       16.98
5     159.6     63.46       102.5       67.17
6       166     54.73         215       49.46

1 回答

  • 11

    数据框是列表 . 即使您只选择一行数据,它仍然是一个列表 .

    试试 unlist . (假设"row"中的所有值当然都是数字 . 如果不是,那么你就会遇到更大的问题 . )

相关问题