As.vector不会将data.frame子集转换为矢量 [英] As.vector does not convert data.frame subset to vector

查看:143
本文介绍了As.vector不会将data.frame子集转换为矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面,我提供了一些我一直在努力的代码片段。我已经成功地将字符串读取为表格。我的表中存在一些子集,希望在其中使用mean()函数。从我所做的研究和我自己的经验来看,mid()尚未为data.frame或data.frame的子集定义行为。因此,希望将我的问题适合某些已定义的行为,我尝试将所需的子集转换为向量。但是,即使使用as.vector转换了我想要的子集之后,我仍然有一个data.frame。当我尝试对此调用中位数时,我得到参数不是数字或逻辑:返回NA。

Below I have provided a snippet of some code that I have been working on. I have been sucessfully reading in string as tables. There exist some subset of my tables which I wish to use the median() function on. From what research I have done and my own experiences median() does not have defined behavior for a data.frame nor a subset of data.frame. So, wishing to fit my problem into some defined behavior, I tried to cast my desired subset into a vector. However, even after using as.vector to cast my desired subset, I still have a data.frame. When I try to call median on this I get "argument is not numeric or logical: returning NA."

我自己玩了很多,并试图在这里和其他地方查找信息。请注意,我已尝试在该线程上的受支持的解决方案中列出的方法 R友好的方式将R data.frame列转换为矢量?,并获得了与我现在相同的结果。我不太在乎我如何做到这一点;随时建议其他方法。

I have played this quite a bit myself and tried to find information here and elsewhere. As a note I have tried the methods listed in the accpeted solution on this thread R-friendly way to convert R data.frame column to a vector? and achieved the same results I have now. I don't care too much how I accomplish this; feel free to suggest other methods.

谢谢您的时间。

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'的示例值:

Here is an example value for '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


推荐答案

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

Data frames are lists. Even if you select just a single row of a data, it's still a list.

尝试取消列表。 (假设行中的所有值都当然是数字。如果不是,则说明问题更大。)

Try unlist. (Assuming all the values in your "row" are of course numeric. If they aren't, that you have bigger problems.)

这篇关于As.vector不会将data.frame子集转换为矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆