[R] data.frame and ddply
arnaud Gaboury
arnaud.gaboury at gmail.com
Fri Apr 16 15:28:37 CEST 2010
Dear group,
Here is my df :
futures <-
structure(list(CONTRAT = c("WHEAT May/10 ", "WHEAT May/10 ",
"WHEAT May/10 ", "WHEAT May/10 ", "COTTON NO.2 May/10 ", "COTTON NO.2 May/10
",
"COTTON NO.2 May/10 ", "PLATINUM Jul/10 ", " SUGAR NO.11 May/10 ",
" SUGAR NO.11 May/10 ", " SUGAR NO.11 May/10 ", " SUGAR NO.11 May/10 ",
" SUGAR NO.11 May/10 ", "ROBUSTA COFFEE (10) May/10 ", "ROBUSTA COFFEE (10)
May/10 ",
"ROBUSTA COFFEE (10) May/10 ", "ROBUSTA COFFEE (10) May/10 ",
"ROBUSTA COFFEE (10) May/10 ", "ROBUSTA COFFEE (10) May/10 ",
"ROBUSTA COFFEE (10) May/10 ", "ROBUSTA COFFEE (10) May/10 ",
"ROBUSTA COFFEE (10) May/10 ", "ROBUSTA COFFEE (10) May/10 ",
"ROBUSTA COFFEE (10) May/10 ", "ROBUSTA COFFEE (10) May/10 "),
QUANTITY = c(1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 2, 1,
1, 1, 2, 1, 1, 1, 1, 2, 1, 1), SETTLEMENT = c("467.7500",
"467.7500", "467.7500", "467.7500", "78.1300", "78.1300",
"78.1300", "1,739.4000", "16.5400", "16.5400", "16.5400",
"16.5400", "16.5400", "1,353.0000", "1,353.0000", "1,353.0000",
"1,353.0000", "1,353.0000", "1,353.0000", "1,353.0000", "1,353.0000",
"1,353.0000", "1,353.0000", "1,353.0000", "1,353.0000")), .Names =
c("CONTRAT",
"QUANTITY", "SETTLEMENT"), row.names = c(NA, 25L), class = "data.frame")
Here is my code :
>opfut=ddply(futures, c("CONTRAT","SETTLEMENT"), summarise, POSITION=
sum(QUANTITY))
Here is the output:
> opfut
CONTRAT SETTLEMENT POSITION
1 SUGAR NO.11 May/10 16.5400 5
2 COTTON NO.2 May/10 78.1300 3
3 PLATINUM Jul/10 1,739.4000 -1
4 ROBUSTA COFFEE (10) May/10 1,353.0000 15
5 WHEAT May/10 467.7500 4
It is almost exactly what I want, except I am expecting the POSITION column
before the SETTLEMENT column. How can I modified my code to obtain this?
TY
***************************
Arnaud Gaboury
Mobile: +41 79 392 79 56
BBM: 255B488F
More information about the R-help
mailing list