Reputation: 11
I have a dataset, there are duplicate observations, how to keep the unique observation?
ID Date Type
1 201301 A
2 201308 B
4 201303 R
1 201301 A
3 201305 C
2 201308 B
What I want is:
ID Date Type
1 201301 A
2 201308 B
4 201303 R
3 201305 C
I tried the unique & duplicated function. But it didn't work.
dataset[which(dataset$ID %in% unique(dataset$ID)),] # will keep all the row
dataset[!duplicated(dataset$ID),] #will only keep the ID=3,4,as follows
ID Date Type
4 201303 R
3 201305 C
How can I get the target dataset in R?
Upvotes: 0
Views: 439
Reputation: 58825
Either
unique(dataset)
or
dataset[!duplicated(dataset),]
will work.
(Copying the answer from the comments into a proper answer).
Upvotes: 2