[R] data.frame and ddply

arnaud Gaboury arnaud.gaboury at gmail.com
Fri Apr 16 16:12:28 CEST 2010


I found a way using the subset command :

>opfut=subset(ddply(futures, c("CONTRAT","SETTLEMENT"), summarise, POSITION=
sum(QUANTITY)),select=c(CONTRAT,POSITION,SETTLEMENT))

> opfut
                      CONTRAT POSITION SETTLEMENT
1         SUGAR NO.11 May/10         5    16.5400
2         COTTON NO.2 May/10         3    78.1300
3            PLATINUM Jul/10        -1 1,739.4000
4 ROBUSTA COFFEE (10) May/10        15 1,353.0000
5               WHEAT May/10         4   467.7500




> -----Original Message-----
> From: Felipe Carrillo [mailto:mazatlanmexico at yahoo.com]
> Sent: Friday, April 16, 2010 4:02 PM
> To: arnaud Gaboury; r-help at r-project.org
> Subject: Re: [R] data.frame and ddply
> 
> You can do something like this after the output from opfut
> opfut <- data.frame(opfut$CONTRAT,opfut$POSITION,opfut$SETTLEMENT)
> names(opfut) <- c('CONTRAT','POSITION','SETTLEMENT')
> opfut
> 
> Felipe D. Carrillo
> Supervisory Fishery Biologist
> Department of the Interior
> US Fish & Wildlife Service
> California, USA
> 
> 
> 
> ----- Original Message ----
> > From: arnaud Gaboury <arnaud.gaboury at gmail.com>
> > To: r-help at r-project.org
> > Sent: Fri, April 16, 2010 6:28:37 AM
> > Subject: [R] data.frame and ddply
> >
> > 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
> 
> ______________________________________________
> > ymailto="mailto:R-help at r-project.org"
> > href="mailto:R-help at r-project.org">R-help at r-project.org mailing list
> > href="https://stat.ethz.ch/mailman/listinfo/r-help" target=_blank
> > >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