hi, there. Here is my problem. I have a data frame by read.table(). I want to take every row from this data frame and compute the correlation between the current row and a fixed vector. finally I want to append the correlation computed as a column to the data frame. this is what i am doing. df<-read.delim("filename", col.names=v) myv<-c(....)#this is my vector that I need to compute cor with corv<-cor(t(df[,c(..)]), myv, use="complete.obs") #only use c(..) in df the values I got from corv doesnt seem to be right when I tried to verify them. the way i am doing this is probably not very smart or totally wrong. Can someone help? thanks very much. Lei Department of Chemsitry University of Washington Box 351700 Seattle, WA 98195 Phone: 206-543-4428 Fax: 206-685-8665 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Lei Jiang wrote:> hi, there. > > Here is my problem. I have a data frame by read.table(). I want to take > every row from this data frame and compute the correlation between the > current row and a fixed vector. finally I want to append the correlation > computed as a column to the data frame. > > this is what i am doing. > df<-read.delim("filename", col.names=v) > myv<-c(....)#this is my vector that I need to compute cor with > corv<-cor(t(df[,c(..)]), myv, use="complete.obs") #only use c(..) in df > > the values I got from corv doesnt seem to be right when I tried to verify > them. the way i am doing this is probably not very smart or totally wrong. > Can someone help?Perhaps the following would do the trick. df$corv <- apply(df,1,function(x) cor(x,myv,use="complete.obs")) -Bill -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Lei Jiang wrote:> > ...Here is my problem. I have a data frame by read.table(). I want to > take every row from this data frame and compute the correlation between > the current row and a fixed vector. finally I want to append the > correlation computed as a column to the data frame.This might do what you want. fixvec<-c(2,4,3,5,4,6,5,7,6,8) test.df<-data.frame(matrix(sample(1:9,100,replace=T),ncol=10)) test.df<-data.frame(test.df,cors=apply(as.matrix(test.df),1,cor,fixvec)) Jim -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._