R duplicate ID variables with different values

2.9k views Asked by At

I have a data frame that looks like this;

head(x)
user_id    location
1          New York
1          Chicago
2          Atlanta
3          San Antonio

I would like to remove the duplicate rows (ie. user_id 1) without regard to their location. So I need a new data frame that only has unique ID's but still has ONE of their locations ( so for ID 1, it doesn't matter if it gets Chicago or New York).

1

There are 1 answers

0
Mamoun Benghezal On BEST ANSWER

you can try

x[!duplicated(x$user_id), ]
  user_id    location
1       1    New_York
3       2     Atlanta
4       3 San_Antonio