[R] sum rows in a data.frame

David Winsemius dwinsemius at comcast.net
Thu Apr 15 14:56:57 CEST 2010


On Apr 15, 2010, at 3:50 AM, arnaud Gaboury wrote:

> 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.


Lacking a reproducible example   ... that is one that can be readily  
pasted into a console session ... this code remains untested and  
probably full of bugs so it is more a pseudo-code approach to the  
problem than a solution:

DESCRIPgrep <- sub(".{13}$",    "",  lme$DESCRIPTION, perl=TRUE)
with( lme, tapply(as.numeric(as.character(CLOSING.PRICE),   
DESCRIPgrep, mean)

Please reread the Posting Guide and submit data examples constructed  
with dput or dump so that responders can work on representative data.  
It would be a real pain to reconstruct that data example but you could  
have trivially offered it for testing.

>
> 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.
--

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list