1

I have a problem, which should not be difficult but given my rudimentary knowledge of R,I am getting stuck. Any help is appreciated (especially a dplyr one :).

Let's say I have two dataframes df and df_new. Now df has 20 observations and df_new has 7 observations, I want to create a new column in dataframe df such that it takes the values from df_new$x. Since df has more observations than df_new, the values should be repeated based on a match between df$let and df_new$let1.

set.seed(7)
df <- data.frame(x=runif(20),let=rep(letters[1:10],each=2))
df_new <- data.frame(x=runif(7),let1=c('a','b','b','c','d','e','e'))

1 Answers1

1

We can do a join

library(dplyr)
left_join(df, df_new, by = c('let' = 'let1'))
akrun
  • 874,273
  • 37
  • 540
  • 662