[R] data frame manipulation

arnaud Gaboury arnaud.gaboury at gmail.com
Fri Apr 16 10:16:30 CEST 2010


Dear group,

Here is my data.frame :


df <-
structure(list(DESCRIPTION = c("PRM HGH GD ALU", "PRM HGH GD ALU", 
"PRIMARY NICKEL", "PRIMARY NICKEL", "PRIMARY NICKEL", "PRIMARY NICKEL", 
"STANDARD LEAD ", "STANDARD LEAD ", "STANDARD LEAD ", "STANDARD LEAD ", 
"STANDARD LEAD ", "STANDARD LEAD ", "STANDARD LEAD ", "STANDARD LEAD ", 
"SPCL HIGH GRAD", "SPCL HIGH GRAD", "SPCL HIGH GRAD", "SPCL HIGH GRAD", 
"SPCL HIGH GRAD", "SPCL HIGH GRAD", "SPCL HIGH GRAD", "SPCL HIGH GRAD", 
"SPCL HIGH GRAD", "SPCL HIGH GRAD"), CREATED.DATE = structure(c(14708, 
14708, 14672, 14673, 14678, 14678, 14700, 14700, 14700, 14700, 
14700, 14700, 14700, 14705, 14707, 14707, 14707, 14708, 14708, 
14708, 14708, 14708, 14622, 14634), class = "Date"), QUANITY = c(-1, 
1, 1, -1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, 1, 
-1, 1, 1, 1, -1), CLOSING.PRICE = c("2,415.9000", "2,415.9000", 
"25,755.7100", "25,755.7100", "25,760.8600", "25,760.8600", "2,355.9600", 
"2,355.9600", "2,355.9600", "2,355.9600", "2,355.9600", "2,355.9600", 
"2,355.9600", "2,357.1200", "2,420.7300", "2,420.7300", "2,420.7300", 
"2,421.0500", "2,421.0500", "2,421.0500", "2,421.0500", "2,421.0500", 
"2,388.4300", "2,388.4300")), .Names = c("DESCRIPTION", "CREATED.DATE", 
"QUANITY", "CLOSING.PRICE"), row.names = 26:49, class = "data.frame")

I am looking at summarize it in something like this :

> op
     DESCRIPTION POSITION       DATE
1 PRIMARY NICKEL        0 2010-03-10
2 PRM HGH GD ALU        0 2010-04-09
3 SPCL HIGH GRAD        2 2010-04-09
4 STANDARD LEAD         0 2010-04-06



To obtain "op", I wrote this following line :

>   op=ddply(df, c("DESCRIPTION"), summarise, POSITION=
sum(QUANITY),DATE=max(CREATED.DATE)).

Until there, fine. But I need to have one more column, "CLOSING.PRICE". If I
write this line :


>   op1=ddply(c, c("DESCRIPTION","CLOSING.PRICE"), summarise, POSITION=
sum(QUANITY),DATE=max(CREATED.DATE))

Here is what I get:


> op1
     DESCRIPTION CLOSING.PRICE POSITION       DATE
1 PRIMARY NICKEL   25,755.7100        0 2010-03-05
2 PRIMARY NICKEL   25,760.8600        0 2010-03-10
3 PRM HGH GD ALU    2,415.9000        0 2010-04-09
4 SPCL HIGH GRAD    2,388.4300        0 2010-01-25
5 SPCL HIGH GRAD    2,420.7300        1 2010-04-08
6 SPCL HIGH GRAD    2,421.0500        1 2010-04-09
7 STANDARD LEAD     2,355.9600       -1 2010-04-01
8 STANDARD LEAD     2,357.1200        1 2010-04-06

Not exactly what I want. Can anyone help?
TY



More information about the R-help mailing list