R语言批量计算t检验,输出pvalue和均值

1.输入数据如下:

2.代码如下

r 复制代码
setwd("E:/R/Rscripts/rG4相关绘图")
# 读取CSV文件
data <- read.csv("box-cds-ABD-不同类型rg4-2.csv", stringsAsFactors = FALSE)


# 筛选出Type2列为指定五种类型的数据
filtered_data <- subset(data, Type2 %in% c("G2L1-2", "G2L1-4", "G3", "G3B", "G3V"))

# 分别提取五种类型对应的Score
scores_list <- list(
  G2L1_2 = filtered_data$Score[filtered_data$Type2 == "G2L1-2"],
  G2L1_4 = filtered_data$Score[filtered_data$Type2 == "G2L1-4"],
  G3 = filtered_data$Score[filtered_data$Type2 == "G3"],
  G3B = filtered_data$Score[filtered_data$Type2 == "G3B"],
  G3V = filtered_data$Score[filtered_data$Type2 == "G3V"]
)

# 初始化一个空的数据框,用于存储结果
results <- data.frame(
  Group1 = character(),
  Group2 = character(),
  Mean1 = numeric(),
  Mean2 = numeric(),
  TStatistic = numeric(),
  PValue = numeric(),
  stringsAsFactors = FALSE
)

# 对所有可能的组合进行两两比较
for (i in 1:(length(scores_list)-1)) {
  for (j in (i+1):length(scores_list)) {
    group1 <- names(scores_list)[i]
    group2 <- names(scores_list)[j]
    scores1 <- scores_list[[i]]
    scores2 <- scores_list[[j]]
    
    # 执行t检验
    test_result <- t.test(scores1, scores2)
    
    # 将结果添加到结果数据框
    results <- rbind(results, data.frame(
      Group1 = group1,
      Group2 = group2,
      Mean1 = mean(scores1),
      Mean2 = mean(scores2),
      TStatistic = test_result$statistic,
      PValue = test_result$p.value
    ))
  }
}
# 输出结果
print(results)

3.输出结果:

相关推荐
青春不败 177-3266-05207 小时前
基于R语言BIOMOD2及机器学习方法的物种分布模拟与案例分析
随机森林·机器学习·r语言·生态学·植被遥感·生物多样性·物种分布
青春不败 177-3266-05202 天前
R语言与DSSAT作物模型
r语言·生态学·作物模型·数字农业·dssat模型·农业科学
asyxchenchong8882 天前
农业系统模拟APSIM全流程详解(气象/土壤/碳氮平衡/NG版本)附R批量处理代码
开发语言·r语言
idolao5 天前
R语言4.4.3统计分析软件安装教程:详细步骤+自定义安装路径(64位)
开发语言·r语言
做cv的小昊6 天前
【TJU】应用统计学——第四周作业(2.3 C-R不等式、2.4区间估计)
c语言·人工智能·算法·机器学习·数学建模·r语言·概率论
爱技术的阿呆8 天前
R code debug 和 study
开发语言·r语言
Q一件事8 天前
R语言制图-相关性及关系网络图
开发语言·r语言
weixin_446934038 天前
多分类暴露变量的亚组分析森林图功能上线了,R语言搞不了风暴统计平台一键搞定
人工智能·机器学习·分类·数据挖掘·r语言
天桥下的卖艺者9 天前
R语言使用TrialEmulation包快速进行数据模拟RCT研究(真实世界研究)
开发语言·r语言·模拟rct