1.输入数据如下:
2.代码如下
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.输出结果: