On Apr 16, 2010, at 9:28 AM, arnaud Gaboury wrote:
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?
Most compact way:
opfut[, c(1,3,2)]
Better readability:
opfut[, c("CONTRAT", "POSITION", "SETTLEMENT")]
And that particular operation is really basic stuff, suggesting that
you should go back to the basic R documents and spend some more time
educating yourself. Either of those extract operations could have been
appended on the ddply call as well, e.g.:
opfut=ddply(futures, c("CONTRAT","SETTLEMENT"), summarise, POSITION=
sum(QUANTITY))[, c(1,3,2)]
opfut
#----------------
CONTRAT POSITION SETTLEMENT
1 SUGAR NO.11 May/10 5 16.5400
2 COTTON NO.2 May/10\n 1 78.1300
3 COTTON NO.2 May/10 2 78.1300
4 PLATINUM Jul/10 -1 1,739.4000
5 ROBUSTA COFFEE (10)\nMay/10 1 1,353.0000
6 ROBUSTA COFFEE (10) May/10 14 1,353.0000
7 WHEAT May/10 4 467.7500
("Functional programming")
--
David Winsemius, MD
West Hartford, CT
______________________________________________
R-help@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.