Reputation: 79
I have df1 with length 100 looks like
PID
123
234
T345
P456
567
and I have another df2 with length 1000 looks like same
PID
123
234
567
T678
P768
....
I need to create new column in df1
if it matches PID
with df2
status as "1"
or "0"
Expected output :
PID V1
123 1
234 1
T345 0
P456 0
567 1
I tried ifelse condition but error occured due to uneven length .
Thanks in advance
Upvotes: 0
Views: 33
Reputation: 101916
You can try %in%
like below
df1$V1 <- +(df1$PID %in% df2$PID)
which gives
> df1
PID V1
1 123 1
2 234 1
3 T345 0
4 P456 0
5 567 1
Data
> dput(df1)
structure(list(PID = c("123", "234", "T345", "P456", "567"),
V1 = c(1L, 1L, 0L, 0L, 1L)), row.names = c(NA, 5L), class = "data.frame")
> dput(df2)
structure(list(PID = c("123", "234", "567", "T678", "P768")), row.names = c(NA,
5L), class = "data.frame")
Upvotes: 0
Reputation: 39603
I would suggest a base R
approach with match()
and the use ifelse
:
#Data
df1 <- structure(list(PID = c("123", "234", "T345", "P456", "567")), class = "data.frame", row.names = 2:6)
df2 <- structure(list(PID = c("123", "234", "567", "T678", "P768")), row.names = 2:6, class = "data.frame")
Now the code using a matching between values and then formating to 0 or 1:
#Match
df1$NewVar <- df2[match(df1$PID,df2$PID),'PID']
df1$NewVar <- ifelse(is.na(df1$NewVar),0,1)
The output:
PID NewVar
1 123 1
2 234 1
3 T345 0
4 P456 0
5 567 1
Upvotes: 1