[R] sum rows in a data.frame...solution

arnaud Gaboury arnaud.gaboury at gmail.com
Thu Apr 15 11:33:34 CEST 2010


Found this solution. It is maybe not the most elegant way, but it does the
job.

  > a=as.data.frame(substr(lme$DESCRIPTION,1,14))
  > colnames(a)=c("DESCRIPTION")
  > lme=as.data.frame(c(a,lme[,2:3]))

  > lme

         DESCRIPTION CLOSING.PRICE POSITION
1  PRIMARY NICKEL   25,755.7100        0
2  PRIMARY NICKEL   25,760.8600        0
3  PRM HGH GD ALU    2,415.9000        0
4  SPCL HIGH GRAD    2,420.1000       -1
5  SPCL HIGH GRAD    2,420.4100       -1
6  SPCL HIGH GRAD    2,420.7300        1
7  SPCL HIGH GRAD    2,421.0500        1
8  SPCL HIGH GRAD    2,388.4300        0
9  SPCL HIGH GRAD    2,389.0000        0
10 SPCL HIGH GRAD    2,389.5700        0
11 SPCL HIGH GRAD    2,402.2900        0
12 SPCL HIGH GRAD    2,402.6400       -2
13 SPCL HIGH GRAD    2,391.8600        0
14 SPCL HIGH GRAD    2,403.0000        2
15 SPCL HIGH GRAD    2,392.4300        0
16 SPCL HIGH GRAD    2,393.0000        0


If someone has a better idea, it is welcomed.


-----Original Message-----
From: arnaud Gaboury [mailto:arnaud.gaboury at gmail.com] 
Sent: Thursday, April 15, 2010 9:50 AM
To: 'r-help at r-project.org'
Subject: sum rows in a data.frame

Dear group,

Here is a data.frame, "lme":

> lme
                          DESCRIPTION CLOSING.PRICE POSITION
4        PRIMARY NICKEL USD 04/06/10    25,755.7100        0
5        PRIMARY NICKEL USD 10/06/10    25,760.8600        0
6  PRM HGH GD ALUMINIUM USD 09/07/10     2,415.9000        0
8  SPCL HIGH GRADE ZINC USD 06/07/10     2,420.1000       -1
9  SPCL HIGH GRADE ZINC USD 07/07/10     2,420.4100       -1
10 SPCL HIGH GRADE ZINC USD 08/07/10     2,420.7300        1
11 SPCL HIGH GRADE ZINC USD 09/07/10     2,421.0500        1
12 SPCL HIGH GRADE ZINC USD 13/04/10     2,388.4300        0
13 SPCL HIGH GRADE ZINC USD 14/04/10     2,389.0000        0
14 SPCL HIGH GRADE ZINC USD 15/04/10     2,389.5700        0
15 SPCL HIGH GRADE ZINC USD 17/05/10     2,402.2900        0
16 SPCL HIGH GRADE ZINC USD 18/05/10     2,402.6400       -2

DESCRIPTION and CLOSING.PRICE are factors, POSITION is numeric.

I want to sum POSITION by products, i.e. PRIMARY NICKEL, PRM HGH GD
ALUMINIUM and SPCL HIGH GRADE ZINC. The problem is that, as you can see,
there is a different date as a part of each product description. Can anyone
tell me how to get rid of these dates so I can sum the position column? The
number of rows is not fixed and will change every day.

TY.



More information about the R-help mailing list