Show matched and unmatched results from vector in data table

Hi All,

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.

Cheers
Steen

1 Like

Thank you steen for your reply.

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!

2 Likes

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.

By the way, you might want to look at FAQ: How to make your code look nice? Markdown Formatting

3 Likes

Hi jdlong,

I have tested it is working fine... Many Thanks for your answer.

I Frank,

yes , match(df$Letter, c("A", "R")) , this will show even for duplicates in column.
Thanks a ton.