【问题标题】:R - Correlation test over all data frames in listR - 对列表中所有数据帧的相关性测试
【发布时间】:2026-02-11 04:15:02
【问题描述】:

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

我希望为相同的两个变量 RainfallOrigin 对每个数据帧执行相关性测试

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

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

【问题讨论】:

    标签: r list lapply


    【解决方案1】:

    lapply 中使用匿名函数:

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

    由于cor 函数将返回单个数字作为输出,您还可以使用sapplycor 中获取向量输出。

    【讨论】:

    • 操,你真了不起
    【解决方案2】:

    我们可以使用map

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

    【讨论】: