Re: [R] extract rows in dataframe with duplicated column values

From: Rob J Goedman <goedman_at_mac.com>
Date: Fri 18 Mar 2005 - 14:35:19 EST

Tiago,

Assuming the column in x is sorted:

t = which(duplicated(x[, 1]))
x[sort(union(t-1, t)),]

or, if not sorted:

t = which(duplicated(sort(x[, 1])))
x[sort(union(t-1, t)),]

Rob

On Mar 17, 2005, at 6:11 PM, Tiago R Magalhaes wrote:

> Hi
>
> I want to extract all the rows in a data frame that have duplicates
> for a given column.
> I would expect this question to come up pretty often but I have
> researched the archives and surprisingly couldn't find anything.
> The best I can come up with is:
>
> x <- data.frame(a=c(1,2,2,3,3,3), b=10)
> xdup1 <- duplicated(x[,1])
> xdup2 <- duplicated(x[,1][nrow(x):1])[nrow(x):1]
> xAllDups <- x[(xdup1+xdup2)!=0,]
>
> This seems to work, but it's so convoluted that I'm sure there's a
> better method.
> Thanks for any help and enlightenment
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

        [[alternative text/enriched version deleted]]



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Mar 18 14:41:24 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:51 EST