R - 列表中所有数据帧的相关性测试

R - Correlation test over all data frames in list

我有一个列表 (top30clean),其中包含标记为 b1b21 的 21 个数据帧,每个数据帧都有三个变量 - StationRainfallOrigin.

我希望针对相同的两个变量对每个数据帧执行相关性测试 Rainfall & Origin

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

我知道上面的代码是错误的,但我不知道从哪里开始

lapply中使用匿名函数:

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

由于 cor 函数将 return 单个数字作为输出,您还可以使用 sapplycor.

中获取矢量输出

我们可以使用map

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