Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a table with values that break up a continuous variable in two groups

Tags:

r

I am looking for your help. I am trying to divide a continuous variable in two groups, I put this example about what I am trying to do:

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))

I try to separe my continuous variable in two groups with an initial value 20, then:

g1=data.frame(x[x$v1>20,])
g2=data.frame(x[x$v1<=20,])

After I compute mean and sd for g1 and g2:

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

After of this, I would like to have a table that show something like this:

Value   Mean.G1  SD.G1  Mean.G2  SD.G2  N.G1 N.G2
20        62.61    26.8   6.21     3.55  13   37

But this table is not only for 20 I would like to build that table for a vector with different values for example a vector with ten elements and that starts in 20 and that increase in steps op 20, a vector like this v=c(10,30,50,70,90,110,130,150,170,190). I wait this question is clear. Thanks

like image 618
Duck Avatar asked Feb 17 '23 03:02

Duck


1 Answers

I would use reshape2 and 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))

Resulting in

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
like image 91
baptiste Avatar answered Feb 19 '23 23:02

baptiste