gpt4 book ai didi

r - 使用将连续变量分成两组的值构建表

转载 作者:行者123 更新时间:2023-12-04 09:42:33 25 4
gpt4 key购买 nike

我正在寻找你的帮助。我试图将一个连续变量分为两组,我把这个例子放在我想要做的事情上:

x=data.frame(v1=c(1,1,2,2,3,4,5,6,9,9,11,2,4,45,67,89,1,1,5,5,5,6,6,6,9,9,9,11,11,8,8,8,51,90,40,15,30,11,8,9,9,1,5,5,100,67,78,98,34,25))

我尝试将我的连续变量分成两组,初始值为 20,然后:
g1=data.frame(x[x$v1>20,])
g2=data.frame(x[x$v1<=20,])

在我计算 g1 和 g2 的均值和 sd 之后:
mean(g1$x.x.v1...20...)=62.61538
mean(g2$x.x.v1....20...)=6.216216
sd(g1$x.x.v1...20...)=26.80963
sd(g2$x.x.v1....20...)=3.55227
length(g1$x.x.v1...20...)= 13
length(g2$x.x.v1....20...)=37

在此之后,我想要一个显示如下内容的表格:
Value   Mean.G1  SD.G1  Mean.G2  SD.G2  N.G1 N.G2
20 62.61 26.8 6.21 3.55 13 37

但这张表不仅适用于 20 我想为具有不同值的向量构建该表,例如一个具有 10 个元素的向量,从 20 开始,并在步骤 op 20 中增加,这样的向量 v=c(10,30,50,70,90,110,130,150,170,190) .
我等这个问题清楚了。谢谢

最佳答案

我会使用 reshape2 和 plyr,

library(plyr) ; library(reshape2)
v=c(10,20,30,50,70,90,110,130,150,170,190) # added 20 for checking
# create new dichotomy id variable
l1 = llply(v, function(.v) transform(x, test = x[["v1"]] <= .v))
names(l1) = v # name list elements for later reference
all = melt(l1, id=c("v1","test")) # merge data.frames together
# summarise the data by groups
results = ddply(all, c("L1","test"), summarise,
mean = mean(v1), sd=sd(v1), length=length(v1))

导致
arrange(results, as.numeric(L1))

L1 test mean sd length
1 10 FALSE 48.500000 32.505656 18
2 10 TRUE 5.343750 2.902828 32
3 20 FALSE 62.615385 26.809633 13
4 20 TRUE 6.216216 3.552270 37
5 30 FALSE 69.000000 23.870484 11
6 30 TRUE 7.307692 5.907862 39
7 50 FALSE 80.000000 17.270950 8
8 50 TRUE 9.619048 10.245647 42
9 70 FALSE 91.000000 8.717798 5
10 70 TRUE 13.088889 16.555447 45
11 90 FALSE 99.000000 1.414214 2
12 90 TRUE 17.625000 23.951747 48
13 110 TRUE 20.880000 28.456655 50
14 130 TRUE 20.880000 28.456655 50
15 150 TRUE 20.880000 28.456655 50
16 170 TRUE 20.880000 28.456655 50
17 190 TRUE 20.880000 28.456655 50

关于r - 使用将连续变量分成两组的值构建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15465799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com