You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将统计检验结果从单列拆分至多列(R语言)

拆分MannKendall检验结果列为多列的可行方法

MannKendall()返回的是S3类对象,不是普通列表,所以unnest_wider没法直接解析。下面提供两种可行的处理方式:

方式一:用tidyverse工具链

先加载所需包:

library(Kendall)
library(dplyr)
library(purrr)

执行数据处理:

# 生成原始数据
a <- c(1:10) 
b <- c(11:20) 
df <- as.data.frame(rbind(a, b)) 

# 生成检验结果列
df$trend <- apply(df, 1, MannKendall)

# 提取tau和p值并合并到原数据框
df <- df %>%
  mutate(
    tau = map_dbl(trend, ~ .x$tau),
    p_value = map_dbl(trend, ~ round(.x$p.value, 2))
  ) %>%
  select(-trend) # 可选:删掉原trend列

方式二:基础R实现(无需额外包)

如果不想用tidyverse,用基础R也能搞定:

library(Kendall)

# 生成原始数据
a <- c(1:10) 
b <- c(11:20) 
df <- as.data.frame(rbind(a, b)) 

# 生成检验结果列
df$trend <- apply(df, 1, MannKendall)

# 批量提取属性值
tau_values <- sapply(df$trend, function(x) x$tau)
p_values <- sapply(df$trend, function(x) round(x$p.value, 2))

# 新增列到数据框
df$tau <- tau_values
df$p_value <- p_values
df$trend <- NULL # 可选:移除原trend列

补充说明

你可以用str(df$trend[[1]])查看MannKendall对象的所有属性,除了tau和p.value,还有S、varS等可以按需提取。

内容的提问来源于stack exchange,提问作者Brenda Cáceres Mejía

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 13:01:12