Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Data Science by (50.2k points)

I am trying to fill in table1 with matching val2 values of table2

table1$New_val2 = table2[table2$pid==table1$pid,]$val2

enter image description here

But I get the warning

longer object length is not a multiple of shorter object length

which is fair enough because the table lengths are not the same.

Please kindly direct me on the correct way to do this.

1 Answer

0 votes
by (108k points)

You can just apply the following command:

merge(table1, table2[, c("pid", "col2")], by="pid")

Add in the all.x=TRUE argument to keep all of the pids in table1 that don't have matches in table2.

You were on the right track. Here's a way of using the match:

table1$val2 <- table2$val2[match(table1$pid, table2$pid)]

If you want to get expert in R Programming visit this R Programming Course.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.6k users

Browse Categories

...