1
\$\begingroup\$

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:

1,2,3,4,3,4,2,1,3,4,6,7,7

I would like to have an output like this:

3,4,2,1,3,4,7

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.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Dec 31, 2012 at 9:46
\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

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
answered Jan 10, 2013 at 13:29
\$\endgroup\$
1
  • \$\begingroup\$ you're right! It is better. With R I have some tendency to do more complicated stuff.. Thank you for your response \$\endgroup\$ Commented Jan 11, 2013 at 11:24
0
\$\begingroup\$

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 :)

answered Jan 6, 2013 at 10:45
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.