0

I have these 2 data frames:

df1=structure(list(name = structure(1:6, levels = c("z", 
"n", "e", "c", "t","h"), class = "factor"), b = c(9, 
5, 4, 3,7, 5), out = c("*", "", 
 "*", "*", "*", "*")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))


df2=structure(list(name = structure(1:6, levels = c("z", 
"z", "z", "c", "c","n"), class = "factor"), o = c(9, 
 5, 4, 3,7, 3)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

I want to add out and b from df1 to df2.

desired output:

   name    o       out    b

 1 z         9   "*"   9
 2 z         5   "*"   9
 3 z         4   "*"   9
 4 c         3   "*"   3
 5 c         7   "*"   3
 6 n         3   ""    5
bic ton
  • 1,284
  • 1
  • 9
  • 16

1 Answers1

0

What you are looking for is a join:

library(dplyr)

left_join(df2,df1,by="name")

results in:

# A tibble: 6 × 4
  name      o     b out  
  <fct> <dbl> <dbl> <chr>
1 z         9     9 "*"  
2 z         5     9 "*"  
3 z         4     9 "*"  
4 c         3     3 "*"  
5 c         7     3 "*"  
6 n         3     5 "" 
nhaus
  • 786
  • 3
  • 13