我有以下数据:
set.seed(10)
len =100
vnum=rnorm(len)
vint=sample(1:10,len,replace=TRUE)
vbin=sample(letters[1:2],len,replace=TRUE)
dd = data.frame(vnum, vint, vbin)
head(dd)
head(dd)
vnum vint vbin
1: 0.01874617 3 a
2: -0.18425254 6 a
3: -1.37133055 7 a
4: -0.59916772 9 b
5: 0.29454513 2 b
6: 0.38979430 7 b
我需要通过t.test来比较vbin分组的vnum。我还需要对vint做同样的事情,并获得如下输出: 需要输出:
mean_a sd_a mean_b sd_b ttest_p.value
vnum
vint
我试过了:
aggregate(vnum~vbin, dd, function(x) mean(x) )
vbin vnum
1 a -0.1390285
2 b -0.1339682
t(aggregate(vnum~vbin, dd, mean ))
[,1] [,2]
vbin "a" "b"
vnum "-0.1390285" "-0.1339682"
但无法在一个输出中获得所有功能。
答案 0 :(得分:1)
定义自己的函数以生成所需的输出:
myt <- function(x, y, data) {
z <- t.test(y ~ x, data)
x <- as.character(substitute(x))
y <- as.character(substitute(y))
c(mean_a = z$estimate[1],
sd_a = sd(dd[[y]][dd[[x]]=="a"]),
mean_b = z$estimate[2],
sd_b = sd(dd[[y]][dd[[x]]=="b"]),
test_p.value = z$p.value)
}
然后你可以为你需要的每个变量运行它:
rbind(myt(vbin, vnum, dd), myt(vbin, vint, dd))
## mean_a.mean in group a sd_a mean_b.mean in group b sd_b test_p.value
## [1,] -0.1390285 0.8427401 -0.1339682 1.042787 0.9788149
## [2,] 6.4509804 2.8587670 5.7551020 3.003683 0.2385821