我有一个特征数据集,每行一个特征。我想执行一个 t.test 比较数据集中两个特定列的平均值,即 T_Mean 和 A_Mean 或第 2 列和第 8 列。我尝试使用 sapply 对此进行编码,但没有运气,示例数据集和代码如下。非常感谢修复我的代码的任何帮助!
WW_Summary <- structure(list(Trait =
c("Morph PC1", "Morph PC2", "Morph PC3", "Morph PC4", "Colour", "Delta15N", "Delta13C"),
T_Mean = c(-0.000369133942377988, -0.478614374395391, -0.0429785751248986, 0.141563333908087, 5.09447415329768, 7.79253141831239, -20.3678994614004),
T_SD = c(1.25617540601557, 0.994922039584068, 0.72787412346719, 0.5683273217636, 1.85452769780342, 1.56401940841295, 2.33461396773921),
T_N = c(615, 615, 615, 615, 561, 557, 557),
HZ_Mean = c(0.379669406453242, 0.307293731157124, -0.0499328976749929, -0.0563021988086238, 4.74712643678161, 8.4568926056338, -20.8209771126761),
HZ_SD = c(1.27837645625113, 1.11890593333031, 0.71490087377916, 0.699316698091669, 1.90101932622734, 1.86547215761457, 1.9590774632374),
HZ_N = c(1137, 1137, 1137, 1137, 1131, 1136, 1136),
A_Mean = c(-0.818704170327851, -0.104449965981942, 0.157885253051751, -0.0437302662392194, 4.31320754716981, 9.79891783567134, -19.955250501002),
A_SD = c(1.29535566832773, 0.97478498249366, 0.678515276691309, 0.563663991917263, 1.63029422418466, 2.06376134152221, 1.47077203157055),
A_N = c(527, 527, 527, 527, 530, 499, 499)),
.Names = c("Trait", "T_Mean", "T_SD", "T_N", "HZ_Mean", "HZ_SD", "HZ_N", "A_Mean", "A_SD", "A_N"),
class = "data.frame", row.names = c(NA, -7L))
## Perform t-test separately for each trait (row) comparing means for T_Mean & A_Mean (columns 2 and 8)
WW_Summary_T <- data.frame(t(sapply(WW_Summary[,c(2,8)], function(temp)
unlist(t.test(temp, alternative = c("two.sided"))[c("statistic",
"parameter", "p.value", "conf.int")]))))