[R] Working with duplicated rows

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Mon Jan 12 17:19:34 CET 2009


try aggregate(), e.g.,

dat <- read.table(textConnection("long   lat   value

10     20      5

6      2       3

27    -3       9

10     20      10

4     -1       0

6      2       9"), header = TRUE)
closeAllConnections()


aggregate(dat["value"], list(Long = dat$long, Lat = dat$lat), sum)


I hope it helps.

Best,
Dimitris


Steve Murray wrote:
> Dear all,
> 
> 
> 
> I have a dataframe of 3 columns, consisting of 'longitude', 'latitude'
> and a corresponding 'value'. Where identical 'longitude' and 'latitude'
> pairs occur more than once, I want their corresponding 'value' to be
> summed and the 'pair' to only appear once.
> 
> 
> 
> For example:
> 
> 
> 
> long   lat   value
> 
> 10     20      5
> 
> 6      2       3
> 
> 27    -3       9
> 
> 10     20      10
> 
> 4     -1       0
> 
> 6      2       9
> 
> 
> 
> 
> 
> would be converted to something like:
> 
> 
> 
> long   lat   value
> 
> 
> 10     20      15
> 
> 
> 6      2       12
> 
> 
> 27    -3       9
> 
> 
> 4     -1       0
> 
> 
> 
> 
> 
> 
> ...as rows 1 and 4, 2 and 6 respectively are matched with respect to
> the 'long' and 'lat' columns. Their values in column 3 are then summed
> and reported as one row in the new dataframe.
> 
> 
> 
> How would I go about coding this in R?
> 
> 
> 
> Many thanks,
> 
> 
> 
> Steve
> 
> 
> _________________________________________________________________
> Are you a PC?  Upload your PC story and show the world
> 
> ______________________________________________
> R-help at 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.
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014




More information about the R-help mailing list