Removing not duplicated rows

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Removing not duplicated rows

Chris82
Hello R users,

I have a problem to delete rows in a table which are not duplicated in order of an id number

a short example:

x <- data.frame(cbind(id=c(1,2,2,2,3,3,4,5,6,6), value=1:10))

x_new <- x[which(duplicated(x$id)),]

> x_new

   id value
3   2     3
4   2     4
6   3     6
10  6    10

The problem is that my command not only delet the id number which occur only one time.
Also the first row with the id number (2,3 and 6 is deleted).

Thanks!

With best regards
Reply | Threaded
Open this post in threaded view
|

Re: Removing not duplicated rows

jthetzel
As I understand it, you are trying to subset the data frame to include only
rows with a non-unique id.

Try this:
x <- data.frame(cbind(id=c(1,2,2,2,3,3,4,5,6,6), value=1:10))
id.table <- table(x$id)
x_new <- subset(x, id %in% id.table[id.table > 1])

Jeremy

______________________________________________
[hidden email] 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.
Jeremy T. Hetzel
Boston University
Reply | Threaded
Open this post in threaded view
|

Re: Removing not duplicated rows

jthetzel
Sorry, I left out the names() function in the last step.

Try this instead:
x <- data.frame(cbind(id=c(1,2,2,2,3,3,4,5,6,6), value=1:10))
id.table <- table(x$id)
x_new <- subset(x, id %in% names(id.table[id.table > 1]))

Jeremy

______________________________________________
[hidden email] 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.
Jeremy T. Hetzel
Boston University
Reply | Threaded
Open this post in threaded view
|

Re: Removing not duplicated rows

Chris82
Thanks a lot, Jeremy!

It's working perfectly.


With best regards