[R] aggregate function output

arun smartpink111 at yahoo.com
Fri Nov 1 15:20:58 CET 2013


You could also try:
library(plyr)
 newdf <- function(.data, ...) {
   eval(substitute(data.frame(...)), .data, parent.frame())
 }

x1 <- ddply(mtcars,.(cyl,gear), newdf, mgp=t(quantile(mpg)),hp=t(quantile(hp))) #(found in one of the google group discussions)
#or



library(data.table)
dt1 <- data.table(mtcars,key=c('cyl','gear'))
 dt2 <- dt1[,c(as.list(quantile(mpg)),as.list(quantile(hp))),by=key(dt1)]
 indx <- grep("%",names(dt2))
 x2 <- as.data.frame(dt2)
names(x2)[indx] <- paste(rep(c("mpg", "hp"),each=5), names(x2)[indx],sep=".")
A.K.





On Friday, November 1, 2013 9:35 AM, arun <smartpink111 at yahoo.com> wrote:
Hi,
Try:
do.call(data.frame,c(x,check.names=FALSE))
A.K.


Hello, 

  

I´m using function aggregate in R 3.0.2.  If I run the instruction 
x<-aggregate(cbind(mpg,hp)~cyl+gear,data=mtcars,quantile) I get the 
result the following data.frame: 

  

cyl 

gear 

mpg.0% 

mpg.25% 

mpg.50% 

mpg.75% 

mpg.100% 

hp.0% 

hp.25% 

hp.50% 

hp.75% 

hp.100% 

4 

3 

21.5 

21.5 

21.5 

21.5 

21.5 

97 

97 

97 

97 

97 

6 

3 

18.1 

18.925 

19.75 

20.575 

21.4 

105 

106.25 

107.5 

108.75 

110 

8 

3 

10.4 

14.05 

15.2 

16.625 

19.2 

150 

175 

180 

218.75 

245 

4 

4 

21.4 

22.8 

25.85 

30.9 

33.9 

52 

64.25 

66 

93.5 

109 

6 

4 

17.8 

18.85 

20.1 

21 

21 

110 

110 

116.5 

123 

123 

4 

5 

26 

27.1 

28.2 

29.3 

30.4 

91 

96.5 

102 

107.5 

113 

6 

5 

19.7 

19.7 

19.7 

19.7 

19.7 

175 

175 

175 

175 

175 

8 

5 

15 

15.2 

15.4 

15.6 

15.8 

264 

281.75 

299.5 

317.25 

335 

  

So far so good, however the strange part happens when I run dim(x) or 
names(x), because the results are 8 4 (dim(x)) and "cyl"  "gear" "mpg" 
"hp" (names(x)). Why this occurs and how do I transform it in a regular 
data.frame with 12 columns? 

  

Thank you in advance, 

  

Daniel 

  

> sessionInfo() 

R version 3.0.2 (2013-09-25) 

Platform: i386-w64-mingw32/i386 (32-bit) 

  

locale: 

[1] LC_COLLATE=Portuguese_Portugal.1252 
LC_CTYPE=Portuguese_Portugal.1252 
LC_MONETARY=Portuguese_Portugal.1252 LC_NUMERIC=C                       


[5] LC_TIME=Portuguese_Portugal.1252     

  

attached base packages: 

[1] grDevices datasets  splines   graphics  stats     tcltk     utils   
methods   base     

  

other attached packages: 

[1] svSocket_0.9-55 TinnR_1.0-5     R2HTML_2.2.1    Hmisc_3.12-2 
Formula_1.1-1   survival_2.37-4 

  

loaded via a namespace (and not attached): 

[1] cluster_1.14.4  grid_3.0.2      lattice_0.20-23 rpart_4.1-3 
svMisc_0.9-69   tools_3.0.2   

  



"Confidencialidade: Esta mensagem (e eventuais ficheiros
anexos) é destinada exclusivamente às pessoas nela indicadas e tem 
natureza confidencial. Se receber esta mensagem por engano, por favor 
contacte o remetente e elimine a mensagem e ficheiros, sem tomar 
conhecimento do respectivo conteúdo e sem reproduzi-la ou divulgá-la. 

Confidentiality Warning: This e-mail message (and any attached 
files) is confidential and is intended solely for the use of the 
individual or entity to whom it is addressed. lf you are not the 
intended recipient of this message please notify the sender and delete 
and destroy all copies immediately."



More information about the R-help mailing list