I need to find matched and unmatched vector values from another vector. Below is the code.
Name <-c("Alex","Rabert", "Lily","Sara")
Letter <-c("A","R","L","S")
df<- data.frame(Name,Letter)
result <- match(c("A", "R"), df$Letter)
df<- data.frame(Name,Letter,result)
print(df)
below is the result from above code. Here i could see 1 and 2 displayed repeatedly which is wrong.
for last two rows result column should have NA NA. Kindly help.
Name Letter result
Alex A 1
Rabert R 2
Lily L 1
Sara S 2
expected as below
Name Letter result
Alex A 1
Rabert R 2
Lily L NA
Sara S NA
Length of result will be 2, so when you make df the second time the result vector will be recycled. The solution depends on what you want to accomplish, this is not evident from your example.
Is there any way to show dynamic matched and unmatched flag as new column that would be included in data table as below? please suggest and share exact code ?
Name Letter result
Alex A 1
Rabert R 2
Lily L NA
Sara S NA
Recycling is tripping you up. You can pad out result with NAs as follows:
ame <-c("Alex","Rabert", "Lily","Sara")
Letter <-c("A","R","L","S")
df<- data.frame(Name,Letter)
result <- match(c("A","R"), df$Letter)
length(result) <- length(Letter) ## make result the same length as Letter. It will pad with NAs
df<- data.frame(Name,Letter,result)
print(df)
I think you'll then get what you expect. Your reproducible example really made it much easier to give you help! Good job!
I suspect that you meant to have those arguments playing opposite roles:
> match(df$Letter, c("A", "R"))
[1] 1 2 NA NA
match looks up its first argument in its second, so generally if you're adding a column to a table, some other column from that table will be passed as the first arg.