有没有一种快速的方法来计算数字矢量的均值和sd,然后将其放到data.frame中?所以假设我有几个看起来像这样的变量
test1 = data.frame(score = c(rnorm(10)), status = c(rep(0, 6), rep(1, 4)))
test2 = data.frame(score = c(rnorm(10)), status = c(rep(0, 6), rep(1, 4)))
test3 = data.frame(score = c(rnorm(10)), status = c(rep(0, 6), rep(1, 4)))
> test1
score status
1 0.6648 0
2 -0.5158 0
3 -0.0297 0
4 -0.1086 0
5 -1.8708 0
6 0.7908 0
7 0.4760 1
8 -0.4841 1
9 -0.3451 1
10 -0.0772 1
我该如何构造一个看起来像这样的data.frame,在每个条目中都带有mean(sd)。我隐约记得我以前用某种命令来完成此操作,但我只是不记得它是什么。而且我不想让每个变量都意味着()和sd(),然后将它们分别放入data.frame中。所以这就是我想要的最终data.frame的样子;东西很长的线:
0 1
test1 0.9(0.1) 0.1(0.03)
test2 0.2(0.1) 0.2(0.03)
test3 0.1(0.2) 0.1(0.04)
下面是我的审判。
myfun()
获取mean(sd)
两个列(或任意数量的列)。数据帧被放入列表中以便在中使用sapply()
。
set.seed(1237)
test1 = data.frame(score = c(rnorm(10)), status = c(rep(0, 6), rep(1, 4)))
test2 = data.frame(score = c(rnorm(10)), status = c(rep(0, 6), rep(1, 4)))
test3 = data.frame(score = c(rnorm(10)), status = c(rep(0, 6), rep(1, 4)))
tests <- list(test1, test2, test3)
myfun <- function(x) {
sapply(x, function(x) paste0(round(mean(x),1),"(",round(sd(x),1),")"))
}
t(sapply(tests, myfun))
score status
[1,] "-0.2(1.1)" "0.4(0.5)"
[2,] "0.3(1.2)" "0.4(0.5)"
[3,] "0.1(0.9)" "0.4(0.5)"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句