I have two data frames, the first has three cols; one a name, the second a value(not important), and the third is blank. This DF contains about 40,000 rows. The second data frame contains a list of names, about 100,000. I wish to match/find the name in the first DF with the name in the second DF and fill in the blank in the first with a value associated with the name in the second DF. I can do this at the moment with a loop but it is slow and not very 'vectorised'. Is there a single line of code that can achieve this. Thanks in advance.
Asked
Active
Viewed 35 times
1 Answers
1
It sounds like you want to merge the the two data frames:
# Create sample data
set.seed(3526)
df1 <- data.frame(
name = letters,
value1 = sample(1:100, 26, replace = TRUE),
stringsAsFactors = FALSE
)
df2 <- data.frame(
name = letters,
value2 = sample(1:100, 26, replace = TRUE),
stringsAsFactors = FALSE
)
# Merge the datasets
new_df <- merge(df1, df2, by = "name")

Mark Timms
- 596
- 3
- 4