Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R编程:计算具有格式的数据帧的两个样本t检验_R - Fatal编程技术网

R编程:计算具有格式的数据帧的两个样本t检验

R编程:计算具有格式的数据帧的两个样本t检验,r,R,晚上好,, 我有以下数据框: Sex A B C D E M 1 20 45 42 12 F 2 10 32 23 43 M 39 32 2 23 43 M 24 43 2 44 12 F 11 3 4 4 11 我将如何使用apply函数,通过性别变量计算上述数据框中每个数值变量的两个样本t检验。结果应该是一个包含五个元素的矩阵 列:F.平均值(女性数值变量的平均值),M.平均值(数值变量的平均值 对于男性),t(对于t统计)、df(对于自由度)和p(对于p值) 谢

晚上好,, 我有以下数据框:

Sex A  B  C D  E
M   1 20 45 42 12
F   2 10 32 23 43
M   39 32 2 23 43
M   24 43 2 44 12
F   11 3 4 4 11
我将如何使用apply函数,通过性别变量计算上述数据框中每个数值变量的两个样本t检验。结果应该是一个包含五个元素的矩阵 列:F.平均值(女性数值变量的平均值),M.平均值(数值变量的平均值 对于男性),t(对于t统计)、df(对于自由度)和p(对于p值)


谢谢

我认为应该是apply、t.test和aggregate的组合。但首先将行名称转换为名称colums。然后,您可以使用aggregate进行子集设置,然后使用t.test进行应用。这里有一个选项,使用
apply
和margin
2

out = apply(data[,-1], 2, function(x){ 
            unlist(t.test(x[data$Sex == 'M'], x[data$Sex == 'F'])[c(1:3,5)],
            recursive=FALSE)
       })

#> out
#                            A           B           C          D          E
#statistic.t         1.2432059  3.35224633 -0.08318328  1.9649783 -0.2450115
#parameter.df        2.5766151  2.82875770  2.70763487  1.9931486  1.8474695
#p.value             0.3149294  0.04797862  0.93946696  0.1887914  0.8309453
#estimate.mean of x 21.3333333 31.66666667 16.33333333 36.3333333 22.3333333
#estimate.mean of y  6.5000000  6.50000000 18.00000000 13.5000000 27.0000000
数据

data = structure(list(Sex = structure(c(2L, 1L, 2L, 2L, 1L), .Label = c("F", 
"M"), class = "factor"), A = c(1L, 2L, 39L, 24L, 11L), B = c(20L, 
10L, 32L, 43L, 3L), C = c(45L, 32L, 2L, 2L, 4L), D = c(42L, 23L, 
23L, 44L, 4L), E = c(12L, 43L, 43L, 12L, 11L)), .Names = c("Sex", 
"A", "B", "C", "D", "E"), class = "data.frame", row.names = c(NA, 
-5L))

你已经试过什么了?一个t.test对象包含所有这些信息OP的问题是在apply函数中的一个子集代码t.test(data.set[data.set[,1]==“F”,]$A,data.set[data.set[,1]==“M”,]$A)对A有效,但使用apply函数在A:E中实现这一点很难。我把Sex作为一个列,结构(Sex=structure)(c(2L,1L,2L,2L,1L),.Label=c(“F”,“M”),class=“factor”),A=c(1,2,39,24,11),B=c(20,10,32,43,3),D=c(45,32,2,2,4),E=c(12,43,43,12,11)),.Names=c(“性别”,“A”,“B”,“D”,“E”),row.Names=c(NA,-5L),class=“data.frame”)unlist的作用是什么?
unlist
将列表简化为一个向量,阅读
?unlist
了解列表的来源,然后意识到t.test输出是一个列表…d'ugh!!!