[R] data frame manipulation ddply

arnaud Gaboury arnaud.gaboury at gmail.com
Tue Jun 1 12:12:34 CEST 2010


Patrick,

When apply to this following df :

futures <-
structure(list(DESCRIPTION = character(0), CREATED.DATE =
structure(numeric(0), class = "Date"), 
    QUANTITY = numeric(0), SETTLEMENT = character(0)), .Names =
c("DESCRIPTION", 
"CREATED.DATE", "QUANTITY", "SETTLEMENT"), row.names = integer(0), class =
"data.frame")


> PosFut <- aggregate(futures$QUANTITY, list(DESCRIPTION =
futures$DESCRIPTION,SETTLEMENT=futures$SETTLEMENT),sum)[,c(1,3,2)]
Error in aggregate.data.frame(as.data.frame(x), ...) : 
  no rows to aggregate



> -----Original Message-----
> From: Patrick Hausmann [mailto:patrick.hausmann at uni-bremen.de]
> Sent: Tuesday, June 01, 2010 11:38 AM
> To: arnaud Gaboury
> Subject: Re: [R] data frame manipulation ddply
> 
> Hi Arnaud,
> 
> maybe "aggregate" can help:
> 
> PosFut <- aggregate(futures$QUANTITY, list(DESCRIPTION =
> futures$DESCRIPTION,
>                                   SETTLEMENT  = futures$SETTLEMENT),
> sum)[, c(1,3,2)]
> 
> HTH,
> Patrick
> 
> Am 01.06.2010 11:02, schrieb arnaud Gaboury:
> > Dear group,
> >
> > Here is my data frame:
> >
> >
> > futures<-
> > structure(list(DESCRIPTION = c("CORN Jul/10", "CORN Jul/10",
> > "CORN Jul/10", "CORN Jul/10", "CORN Jul/10", "LIVE CATTLE Aug/10",
> > "LIVE CATTLE Aug/10", "SUGAR NO.11 Jul/10", "SUGAR NO.11 Jul/10",
> > "SUGAR NO.11 Jul/10", "SUGAR NO.11 Jul/10", "SUGAR NO.11 Jul/10"
> > ), CREATED.DATE = structure(c(18403, 18406, 18406, 18406, 18406,
> > 18407, 18408, 18406, 18407, 18407, 18407, 18407), class = "Date"),
> >      QUANTITY = c(1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1), SETTLEMENT =
> > c("373.2500",
> >      "373.2500", "373.2500", "373.2500", "373.2500", "90.7750",
> >      "90.7750", "14.9200", "14.9200", "14.9200", "14.9200", "14.9200"
> >      )), .Names = c("DESCRIPTION", "CREATED.DATE", "QUANTITY",
> > "SETTLEMENT"), row.names = c(NA, 12L), class = "data.frame")
> >
> > Here is the line I pass :
> >
> >> PosFut=ddply(futures, c("DESCRIPTION","SETTLEMENT"), summarise,
> POSITION=
> > sum(QUANTITY))[,c(1,3,2)]
> >
> > And here the result :
> >
> > PosFut<-
> > structure(list(DESCRIPTION = structure(1:3, .Label = c("CORN Jul/10",
> > "LIVE CATTLE Aug/10", "SUGAR NO.11 Jul/10"), class = "factor"),
> >      POSITION = c(5, 4, 5), SETTLEMENT = structure(c(2L, 3L, 1L
> >      ), .Label = c("14.9200", "373.2500", "90.7750"), class =
> "factor")),
> > .Names = c("DESCRIPTION",
> > "POSITION", "SETTLEMENT"), class = "data.frame", row.names = c(NA,
> > -3L))
> >
> > I can no more use ddply, as this above command line is in a function,
> and
> > this line should be able to work with a data frame with zero rows,
> and in
> > this case ddply doesn't work.
> > Any suggestion how to obtain the same result without ddply?
> >
> > TY for any help.
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> > and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list