dataframe - Calculating correlation in data frame in R -


i have data frame d, has 3 columns, s, n, id , need calculate correlation between "s" , "n" based on "id". eg data frame:

"s"   "n"   "id" 1.6    0.5   2 2.5    0.8   2 4.8    0.7   3 2.6    0.4   3 3.5    0.66  3 1.2    0.1   4 2.5    0.45  4 

so, want calcualte correlation of 2's, 3's , 4's , return vector like:

cor 0.18 0.45 0.65 

my problem how choose these id's , calculate correlation , return in form of vector.

thank you

tab_split<-split(mydf,mydf$id) # list each element subset of data.frame same id  unlist(lapply(tab_split,function(tab) cor(tab[,1],tab[,2]))) # vector of correlation coefficients 

with sample gave :

mydf<-structure(list(s = c(1.6, 2.5, 4.8, 2.6, 3.5, 1.2, 2.5),                       n = c(0.5,0.8, 0.7, 0.4, 0.66, 0.1, 0.45),                       id = c(2l, 2l, 3l, 3l, 3l, 4l,4l)),                  .names = c("s", "n", "id"),                  class = "data.frame",                  row.names = c(na, -7l))  > unlist(lapply(tab_split,function(tab) cor(tab[,1],tab[,2])))        2        3        4  1.000000 0.875128 1.000000 

nb: if column names "n" , "s", can do

unlist(lapply(tab_split,function(tab) cor(tab$s,tab$n))) 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -