3

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."

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.

Thank you for your time.

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"

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
Community
  • 1
  • 1
order
  • 335
  • 1
  • 7
  • 14
  • Please post a reproducible example. What does your data look like? What do you mean by *reading string in as tables?* I'm picturing [tatting](http://en.wikipedia.org/wiki/Tatting) – mnel Dec 17 '12 at 23:20
  • @mnel Reading a string as a table, by that I mean this: `read.table(textConnection(text_array[i]), sep="\t",row.names=NULL, header= FALSE, fill=TRUE). Basically using text connections rather than files.` I mostly mentioned this since many people aren't familiar with that function. I will edit the question show what my data looks like. – order Dec 17 '12 at 23:26
  • Your data does not appear to have read in properly. Are your first two rows really column names, then do the data appear. A data.frame will not handle that. – mnel Dec 17 '12 at 23:36

1 Answers1

15

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.)

joran
  • 169,992
  • 32
  • 429
  • 468
  • 1
    Thanks for reminding me of something simple. Using unlist resolved the problem. No I wasn't trying to find the median of strings or anything of the sort-- just floats I swear. Thanks again. – order Dec 17 '12 at 23:41
  • 1
    I actually think this is a classic R trap/"WTF" opportunity: deserves to be filed under http://stackoverflow.com/questions/1535021/whats-the-biggest-r-gotcha-youve-run-across for confusingness, although the workaround is pretty straightforward. – Ben Bolker Dec 18 '12 at 01:21