r - Computing correlation of vectors by factor label -


i have have 2 data frames. first one, df1, matrix of vectors labeled columns, following:

df1 <- data.frame(a=rnorm(10), b=rnorm(10), c=rnorm(10), d=rnorm(10), e=rnorm(10))  > df1                   b          c           d          e -0.3200306  0.4370963 -0.9146660  1.03219577  0.5215359 -0.3193144  0.8900656 -1.1720264 -0.42591761  0.1936993  0.4897262 -1.3970806  0.6054637  0.12487936  1.0149530  0.3772420  0.8726322  0.3250020 -0.36952560 -0.5447512 -0.6921561 -0.6734468  0.3500812 -0.53373720 -0.6129472  0.2540649 -1.1911106 -0.3266428  0.14013437  1.0830148  0.6606825 -0.8942715  1.1099637 -1.52416540 -0.2383048  1.4767074 -2.1492360  0.2441242 -0.36136344  0.5589114 -0.5338117 -0.2357821  0.7694879 -0.21652356  0.3185631  3.4215916 -0.3157938  0.8895597  0.09946069 -1.0961730 

the second data frame, df2, contains items match colnames of df1. example:

group <- c("1", "1", "2", "2", "3", "3") s1 <- c("a", "d", "e", "c", "b", "d") s2 <- c("d", "b", "a", "c", "b", "a") s3 <- c("b", "c", "a", "e", "e", "a") df2 <- data.frame(group,s1, s2, s3)  > df2  group s1 s2 s3    1   d  b    1  d  b  c    2  e      2  c  c  e    3  b  b  e    3  d   

i compute correlations between column vectors in df1 correspond labeled items in df2. specifically, vectors match cor(df2$s1, df2$s2) , cor(df2$s1, df2$s3).

the output should this:

group s1 s2 s3   cor.s1.s2   cor.s1.s3   1   d  b 0.003825055  -0.2817946   1  d  b  c  -0.2817946  -0.4928023   2  e    -0.3856809  -0.3856809   2  c  c  e           1  -0.3862433   3  b  b  e           1  -0.3888541   3  d   0.003825055 0.003825055 

i've been trying resolve cbind[] keep running problems such 'x' must numeric error cor. in advance help!

you can mapply().

my.cor <- function(x,y) {   cor(df1[,x],df1[,y]) }  df2$cor.s1.s2 <- mapply(my.cor,df2$s1,df2$s2) df2$cor.s2.s3 <- mapply(my.cor,df2$s2,df2$s3) 

Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -