[R] Fwd: How to conditionally remove dataframe rows?
David Winsemius
dwinsemius at comcast.net
Thu Mar 7 02:23:17 CET 2013
On Mar 6, 2013, at 3:21 PM, Francisco Carvalho Diniz wrote:
> Hi,
>
> I have a data frame with two columns. I need to remove duplicated rows in
> first column, but I need to do it conditionally to values of the second
> column.
>
> Example:
>
> Point_counts Psi_Sp
>
> 1 A 0
> 2 A 1
> 3 B 1
> 4 B 2
> 5 B 0
> 6 C 1
> 7 D 1
> 8 D 2
>
>
> I need to turn this data frame in one without duplicated rows at
> point-counts (one visit per point) but maintain the ones with maximum value
> at Psi_Sp, e.g. remove row 1 and maintain 2 or remove rows 3 and 5 and
> maintain 4. At the end I want a data frame like the one below:
>
Try this:
dfrm <- dfrm[ order(dfrm[[1]], -dfrm[[2]] ) , ]
#put desired rows at top of each Point_counts category
# then take top item in each category
dfrm[ !duplicated(dfrm[[1]]) , ]
> Point_counts Psi_Sp
>
> 1 A 1
> 2 B 2
> 3 C 0
> 4 D 2
>
> How can I do it? I found several ways to edit data frames, but
> unfortunately I cound not use none of them.
>
> I appreciate
>
--
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list