Dear useRs,
Given this code I end up with a list of class "by":
a <- sample(1:5,200,replace=TRUE)
b <- sample(c("v1","v2","v3"),200,replace=TRUE)
c <- sample(c(11,22,33),200,replace=TRUE)
data <- runif(200)
grouped <- by(data,list(a,b,c),function(x) {c(min=min(x),max=max(x),
median=round(median(x),digits=2),mean=round(mean(x),digits=2))})
dfr <- do.call("rbind",grouped) #the levels are missing
#----------
grouped
typeof(grouped)
class(grouped)
dimnames(grouped)
How do I get at the levels of the 'group by' variables for each
subset? For example, from this part of the "by" list I want 4, v2 and
33:
: 4
: v2
: 33
min max median mean
0.3897450 0.9215315 0.7300000 0.6700000
---------------------------------------
Thank you,
b.
Here is a different approach to achieving what I think you want using
summarize() in the Hmisc package:
library(Hmisc)
mydata <- data.frame(a = sample(1:5,200,replace=TRUE),
b =
sample(c("v1","v2","v3"),200,replace=TRUE),
c = sample(c(11,22,33),200,replace=TRUE),
y = runif(200))
attach(mydata)
summarize(y, llist(a, b, c),
function(x){c(min=min(x),
max=max(x),
median=round(median(x),digits=2),
mean=round(mean(x),digits=2))},
stat.name="min")
detach(mydata)
bogdan romocea wrote:> Dear useRs,
>
> Given this code I end up with a list of class "by":
>
> a <- sample(1:5,200,replace=TRUE)
> b <-
sample(c("v1","v2","v3"),200,replace=TRUE)
> c <- sample(c(11,22,33),200,replace=TRUE)
> data <- runif(200)
> grouped <- by(data,list(a,b,c),function(x) {c(min=min(x),max=max(x),
> median=round(median(x),digits=2),mean=round(mean(x),digits=2))})
> dfr <- do.call("rbind",grouped) #the levels are missing
> #----------
> grouped
> typeof(grouped)
> class(grouped)
> dimnames(grouped)
>
> How do I get at the levels of the 'group by' variables for each
> subset? For example, from this part of the "by" list I want 4, v2
and
> 33:
> : 4
> : v2
> : 33
> min max median mean
> 0.3897450 0.9215315 0.7300000 0.6700000
> ---------------------------------------
>
> Thank you,
> b.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
>
--
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 452-1424 (M, W, F)
fax: (917) 438-0894
I think these are not stored by "by()" and they are produced inside
"print.by()". One way to get them could be:
dn <- dimnames(grouped)
dn <- expand.grid(dn)
dn <- dn[!sapply(grouped[1:nrow(dn)], is.null), ]
dn
rownames(dfr) <- apply(dn, 1, paste, collapse = "-")
dfr
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
----- Original Message -----
From: "bogdan romocea" <br44114 at gmail.com>
To: <R-help at stat.math.ethz.ch>
Sent: Wednesday, June 08, 2005 4:56 PM
Subject: [R] get level combinations from "by" list
> Dear useRs,
>
> Given this code I end up with a list of class "by":
>
> a <- sample(1:5,200,replace=TRUE)
> b <-
sample(c("v1","v2","v3"),200,replace=TRUE)
> c <- sample(c(11,22,33),200,replace=TRUE)
> data <- runif(200)
> grouped <- by(data,list(a,b,c),function(x) {c(min=min(x),max=max(x),
> median=round(median(x),digits=2),mean=round(mean(x),digits=2))})
> dfr <- do.call("rbind",grouped) #the levels are missing
> #----------
> grouped
> typeof(grouped)
> class(grouped)
> dimnames(grouped)
>
> How do I get at the levels of the 'group by' variables for each
> subset? For example, from this part of the "by" list I want 4, v2
> and
> 33:
> : 4
> : v2
> : 33
> min max median mean
> 0.3897450 0.9215315 0.7300000 0.6700000
> ---------------------------------------
>
> Thank you,
> b.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>