As.vector does not convert data.frame subset to vector
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
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.)
上一篇: 子集r中的多个列