I have a column named 'A'
A |
---|
Dog |
Cat |
Dog |
Sheep |
I want to create a new column, called 'Keep' that has a 1 for the first instance of a duplicate as well as all unique values
A | Keep |
---|---|
Dog | 1 |
Cat | 1 |
Dog | 0 |
Sheep | 1 |
I have a column named 'A'
A |
---|
Dog |
Cat |
Dog |
Sheep |
I want to create a new column, called 'Keep' that has a 1 for the first instance of a duplicate as well as all unique values
A | Keep |
---|---|
Dog | 1 |
Cat | 1 |
Dog | 0 |
Sheep | 1 |
We can use duplicated
:
df$Keep <- as.integer(!duplicated(df$A))
If you want to do this using data.table
:
library(data.table)
setDT(df)[, Keep := as.integer(!duplicated(A))]
df
# A Keep
#1: Dog 1
#2: Cat 1
#3: Dog 0
#4: Sheep 1
data
df <- structure(list(A = c("Dog", "Cat", "Dog", "Sheep")),
class = "data.frame", row.names = c(NA, -4L))