Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a dataframe with several columns. One of them is an user ID column, in this column, I have several ids that can be repeated several times.

What I want to do is remove the first ID, for instance:


I would like to have an output like this:


Where is what I have done:

#find first duplicated of the each user
dup <- duplicated(results$user)

#create other data frame, every time vector is TRUE add the row to new dataframe
results1 <- NULL

for(i in 1:length(results$user)){
    if (dup[i] == TRUE) {
        rbind(results1, results[i,]) -> results1

Since I'm more used to think in Python, I have a feeling this is a very ugly solution for R. I would like to have some feedback, as well as some pointers on how to improve this piece of code.

share|improve this question
up vote 4 down vote accepted

Here's a more efficient solution:

# an example data frame
results <- data.frame(user = c(1,2,3,4,3,4,2,1,3,4,6,7,7), a = 1)

# the solution
results[duplicated(results$user), ]

How it works: duplicated returns a logical vector indicating whether a value was also present at a preceding position in the vector (for each value of results$user).

This logical index is used to choose the appropriate lines of the orginal data frame. This is achieved by using this vector as the first argument for [ and using an empty second argument (to select all columns).

The result:

   user a
5     3 1
6     4 1
7     2 1
8     1 1
9     3 1
10    4 1
13    7 1
share|improve this answer
you're right! It is better. With R I have some tendency to do more complicated stuff.. Thank you for your response – pavid Jan 11 '13 at 11:24

Well after reading some stuffs, I've come to the conclusion that I could eliminate several lines and do this instead:

rbind(results1, results[dup,]) -> results1

It is much quicker and seems more efficient.

However any suggestions or recommendations are welcome :)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.