Skip to content
Prev 166730 / 398502 Next

Working with duplicated rows

One of undoubtedly many ways:

# kind of a pain to delete the blank rows. Try to give us full example  
next time?

 > txt<- "long   lat   value
+ 10     20      5
+ 6      2       3
+ 27    -3       9
+ 10     20      10
+ 4     -1       0
+ 6      2       9
+ "
 > DF2 <- read.table(textConnection(txt), header=TRUE)

 > DF3 <- data.frame(with(DF2,list(lon=long, lat=lat,  
value=ave(value,long, lat, FUN=sum))))

 > DF3
   lon lat value
1  10  20    15
2   6   2    12
3  27  -3     9
4  10  20    15
5   4  -1     0
6   6   2    12

If you want the column name to be "long", the fix is obvious.