Re: [R] data frame manipulation ddply

From: arnaud Gaboury <arnaud.gaboury_at_gmail.com>
Date: Tue, 01 Jun 2010 12:12:34 +0200

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.



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. Received on Tue 01 Jun 2010 - 10:14:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 01 Jun 2010 - 10:30:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive