Home > database >  Running Hmisc::rcorr() on a list of dataframes using lapply
Running Hmisc::rcorr() on a list of dataframes using lapply

Time:03-30

I'm trying to run a Pearson correlation test for every two pairs of fields in each dataframe in a list of dataframes.

data(mtcars)
split_mtcars  = split (mtcars, f= mtcars$gear) 
lapply (split_mtcars,FUN = function (x) 
    rcorr(as.matrix(split_mtcars$x %>% select (-gear,-am)))) #remove `gear` and `am` from matrix

I'm getting the following error:

Error in UseMethod("select") : 
no applicable method for 'select' applied to an object of class "NULL"

When I try to avoid using select:

lapply (split_mtcars,FUN = function (x) rcorr(as.matrix(split_mtcars$x)))

I get the following error:

Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x), : 
'data' must be of a vector type, was 'NULL'

CodePudding user response:

When we loop over a list, the lambda function x returns the object itself - i.e. x is the data.frame element in the list. Thus, split_mtcars$x will work only if we are looping over the names of the split_mtcars object

library(dplyr)
library(Hmisc)
lapply(split_mtcars, function(x) rcorr(as.matrix(x %>% 
           select(-gear, -am))) )

-output

$`3`
       mpg   cyl  disp    hp  drat    wt  qsec    vs  carb
mpg   1.00 -0.65 -0.72 -0.74  0.16 -0.78  0.49  0.65 -0.85
cyl  -0.65  1.00  0.74  0.75 -0.31  0.57 -0.79 -0.93  0.68
disp -0.72  0.74  1.00  0.75 -0.24  0.85 -0.55 -0.68  0.72
hp   -0.74  0.75  0.75  1.00  0.19  0.65 -0.81 -0.78  0.95
drat  0.16 -0.31 -0.24  0.19  1.00 -0.27 -0.19  0.09  0.12
wt   -0.78  0.57  0.85  0.65 -0.27  1.00 -0.25 -0.53  0.76
qsec  0.49 -0.79 -0.55 -0.81 -0.19 -0.25  1.00  0.84 -0.66
vs    0.65 -0.93 -0.68 -0.78  0.09 -0.53  0.84  1.00 -0.73
carb -0.85  0.68  0.72  0.95  0.12  0.76 -0.66 -0.73  1.00

n= 15 


P
     mpg    cyl    disp   hp     drat   wt     qsec   vs     carb  
mpg         0.0094 0.0022 0.0016 0.5712 0.0006 0.0639 0.0089 0.0000
cyl  0.0094        0.0017 0.0014 0.2578 0.0256 0.0004 0.0000 0.0050
disp 0.0022 0.0017        0.0012 0.3948 0.0000 0.0323 0.0050 0.0024
hp   0.0016 0.0014 0.0012        0.5017 0.0087 0.0002 0.0006 0.0000
drat 0.5712 0.2578 0.3948 0.5017        0.3230 0.4896 0.7511 0.6684
wt   0.0006 0.0256 0.0000 0.0087 0.3230        0.3697 0.0442 0.0011
qsec 0.0639 0.0004 0.0323 0.0002 0.4896 0.3697        0.0000 0.0075
vs   0.0089 0.0000 0.0050 0.0006 0.7511 0.0442 0.0000        0.0018
carb 0.0000 0.0050 0.0024 0.0000 0.6684 0.0011 0.0075 0.0018       

$`4`
       mpg   cyl  disp    hp  drat    wt  qsec    vs  carb
mpg   1.00 -0.67 -0.90 -0.88  0.56 -0.82  0.26  0.31 -0.75
cyl  -0.67  1.00  0.77  0.77 -0.32  0.56 -0.59 -0.63  0.94
disp -0.90  0.77  1.00  0.81 -0.63  0.91 -0.21 -0.44  0.86
hp   -0.88  0.77  0.81  1.00 -0.49  0.73 -0.32 -0.37  0.77
drat  0.56 -0.32 -0.63 -0.49  1.00 -0.69 -0.05  0.21 -0.25
wt   -0.82  0.56  0.91  0.73 -0.69  1.00  0.10 -0.10  0.66
qsec  0.26 -0.59 -0.21 -0.32 -0.05  0.10  1.00  0.64 -0.49
vs    0.31 -0.63 -0.44 -0.37  0.21 -0.10  0.64  1.00 -0.60
carb -0.75  0.94  0.86  0.77 -0.25  0.66 -0.49 -0.60  1.00

n= 12 


P
     mpg    cyl    disp   hp     drat   wt     qsec   vs     carb  
mpg         0.0172 0.0000 0.0002 0.0589 0.0010 0.4126 0.3223 0.0052
cyl  0.0172        0.0031 0.0034 0.3183 0.0600 0.0423 0.0273 0.0000
disp 0.0000 0.0031        0.0014 0.0293 0.0000 0.5168 0.1482 0.0003
hp   0.0002 0.0034 0.0014        0.1042 0.0065 0.3095 0.2367 0.0037
drat 0.0589 0.3183 0.0293 0.1042        0.0124 0.8709 0.5036 0.4314
wt   0.0010 0.0600 0.0000 0.0065 0.0124        0.7463 0.7652 0.0192
qsec 0.4126 0.0423 0.5168 0.3095 0.8709 0.7463        0.0238 0.1041
vs   0.3223 0.0273 0.1482 0.2367 0.5036 0.7652 0.0238        0.0402
carb 0.0052 0.0000 0.0003 0.0037 0.4314 0.0192 0.1041 0.0402       

$`5`
       mpg   cyl  disp    hp  drat    wt  qsec    vs  carb
mpg   1.00 -0.96 -0.88 -0.90  0.22 -0.99  0.98  0.76 -0.81
cyl  -0.96  1.00  0.95  0.96 -0.28  0.94 -1.00 -0.56  0.77
disp -0.88  0.95  1.00  0.90 -0.01  0.86 -0.93 -0.52  0.55
hp   -0.90  0.96  0.90  1.00 -0.42  0.92 -0.94 -0.45  0.83
drat  0.22 -0.28 -0.01 -0.42  1.00 -0.25  0.25 -0.21 -0.70
wt   -0.99  0.94  0.86  0.92 -0.25  1.00 -0.96 -0.76  0.85
qsec  0.98 -1.00 -0.93 -0.94  0.25 -0.96  1.00  0.62 -0.77
vs    0.76 -0.56 -0.52 -0.45 -0.21 -0.76  0.62  1.00 -0.51
carb -0.81  0.77  0.55  0.83 -0.70  0.85 -0.77 -0.51  1.00

n= 5 


P
     mpg    cyl    disp   hp     drat   wt     qsec   vs     carb  
mpg         0.0092 0.0486 0.0375 0.7252 0.0013 0.0035 0.1382 0.0976
cyl  0.0092        0.0153 0.0094 0.6453 0.0165 0.0004 0.3273 0.1302
disp 0.0486 0.0153        0.0399 0.9898 0.0622 0.0216 0.3694 0.3406
hp   0.0375 0.0094 0.0399        0.4819 0.0288 0.0195 0.4481 0.0816
drat 0.7252 0.6453 0.9898 0.4819        0.6845 0.6805 0.7352 0.1908
wt   0.0013 0.0165 0.0622 0.0288 0.6845        0.0111 0.1324 0.0691
qsec 0.0035 0.0004 0.0216 0.0195 0.6805 0.0111        0.2615 0.1277
vs   0.1382 0.3273 0.3694 0.4481 0.7352 0.1324 0.2615        0.3751
carb 0.0976 0.1302 0.3406 0.0816 0.1908 0.0691 0.1277 0.3751       
  • Related