简体   繁体   中英

R - Correlation test over all data frames in list

I have a list (top30clean) containing 21 data frames labelled b1 to b21 , each with three variables - Station , Rainfall , Origin .

I was hoping to perform the correlation test for each dataframe for the same two variables Rainfall & Origin

cor <- lapply(top30clean, cor(top30clean$Rainfall, top30clean$Origin, method = c('pearson')

I know the above code is wrong af, but I'm not sure where to start

Use an anonymous function in lapply :

cor <- lapply(top30clean, function(x) cor(x$Rainfall, x$Origin, method = 'pearson'))

Since cor function would return a single number as output you can also use sapply to get vector output in cor .

We can use map

library(purrr)
map(top30clean, ~  cor(.x$Rainfall, .x$Origin, method = 'pearson'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM