Search code examples
rcorrelationhmiscpearson

R - how to limit output for hmisc rcorr?


I have two data frames motivation_on with 60 observations and motivation_off with 146 observations, each consists of 21 vars and 1 ID column, which is in the first column.

Now I want to know how the vars correlate with each other so I use:

rcorr(as.matrix(motivation_on[2:ncol(motivation_on)]), type = "spearman")

and

rcorr(as.matrix(motivation_off[2:ncol(motivation_off)]), type = "spearman")

(The subsetting is done to get rid of the ID column)

Now I want to compute the correlation between the online and offline vars, so I tried:

rcorr(as.matrix(motivation_off[2:ncol(motivation_off)]), as.matrix(motivation_on[2:ncol(motivation_on)]) , type = "spearman")

Now I get what I want but in addition it also shows all the correlations for vars within motivation_on and within motivation_off, which I computed before. This makes the output extremely long. How to get the rcorr output for correlations of on_off exclusively?

edit for clarification: Try the following:

x <- as.matrix(mtcars[1:3])
y <- as.matrix(mtcars[4:6])
rcorr(x,y)

What I wanted is a correlation table with: mpg, cyl, disp as rows and hp, drat, wt as columns and not the full output. my current work around:

z <- rcorr(x,y)
q <- as.data.frame(z[1])
q[1:3,4:6]

Solution

  • It seems that hmisc's rcorr does not provide an option to extract what I want. One can only extract the results manually like I demonstrated in my mtcars example. However the psych::corr.test(x, y) provides exactly the output I wanted - thanks to @user20650 for pointing this out!