[R] Choosing the larger of row pairs

Peter Alspach Peter.Alspach at plantandfood.co.nz
Thu Mar 25 03:58:46 CET 2010


Tena koe Jim

One solution:

> jim
  key value
1   1     1
2   2     0
3   2     2
4   3     0
5   4     0
6   5     1
7   6     3
8   6     2
9   7     0
> tt <- rle(jim$key)$lengths
> ttJim <- jim[cumsum(tt)-tt+tapply(jim$value, jim$key, which.max),]
> ttJim
  key value
1   1     1
3   2     2
4   3     0
5   4     0
6   5     1
7   6     3
9   7     0

HTH ....

Peter Alspach
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of James Rome
> Sent: Thursday, 25 March 2010 2:11 p.m.
> To: r-help at r-project.org
> Subject: [R] Choosing the larger of row pairs
> 
> I have a data frame with some rows that are almost, but not quite
> duplicates of each other.
> By using duplicated(key) on one column and on the column reversed, I
> can
> get 2 columns in my data frame that have adjacent rows with TRUE. For
> example
> key        value         d                di                 ......
> OTHER COLUMNS
> 1             1              FALSE      FALSE
> 2             0              FALSE      TRUE         #Eliminate this
> row
> 2             2              TRUE        FALSE
> 3             0              FALSE      FALSE
> 4             0              FALSE      FALSE
> 5             1              FALSE      FALSE
> 6             3              FALSE      TRUE
> 6             2              TRUE        FALSE     #Eliminate this row
> 7             0              FALSE      FALSE
> . . .
> 
> I want to eliminate the row with the lower value in adjacent rows that
> have TRUE. And in principal, I could have more than 2 duplicate rows
> next to each other.
> 
> How can I do this?
> 
> Thanks,
> Jim
> 
> ______________________________________________
> 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.



More information about the R-help mailing list