0

I have two dataframes df1 and df2 with same number of rows (~2000) and different number of columns. I wanted to do rowwise correlation between these two dataframes. I tried the following codes which I found from the answers in the stack overflow by others, but nothing worked. Is their a solution to this problem?

Df1:
    v1    v2  v3
a1  0.4  0.4 0.3
a2  0.2  0.1 0.5
a3  0.7  0.5 0.6
a4  0.3  0.5 0.9

Df2:

 v1    v2  
a1  0.2  0.4 
a2  0.5  0.9 
a3  0.8  0.5 
a4  0.2  0.6 


cA <- rowMeans(df1)
cB <- rowMeans(df2)
sA <- sqrt(rowMeans(cA^2))
sB <- sqrt(rowMeans(cB^2))
rowMeans(cA * cB) / (sA * sB)

The error shows after the sqrt step :

Error in base::rowMeans(x, na.rm = na.rm, dims = dims, ...) : 'x' must be an array of at least two dimensions

This also din work:

 sapply(1:nrow(df1), function(i) cor(df1[i,], df2[i,]))

I highly appreciate your help!!

Gene
  • 21
  • 1
  • 5
  • 3
    If they have a different number of columns, how do you plan to calculate the correlation? It would help if you provided a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and the desired output. – MrFlick May 08 '17 at 15:38
  • `sapply(1:nrow(df2), function(i) cor(df1[i,], df2[i,]))` should work – cirofdo May 08 '17 at 16:31
  • `cor(c(0.4, 0.4, 0.3), c(0.2, 0.4))` isn't defined. Not sure what you are expecting. Each vector needs to have the same number of elements to calculate the correlation. – MrFlick May 08 '17 at 17:40

0 Answers0