[R] data frame manipulation ddply
arnaud Gaboury
arnaud.gaboury at gmail.com
Tue Jun 1 11:02:10 CEST 2010
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.
More information about the R-help
mailing list