r - Merge two tables with same id -
this question has answer here:
i have data frame this:
df1: x1 x2 x3 1 1 4 7 2 2 5 8 3 3 6 9 class(df1[1,"x1"]) [1] "character"
now have data frame this:
df2: id name 1 1 2 2 c 3 3 b 4 4 z 5 5 x 6 6 g 7 7 e 8 8 y 9 9 h the id here numeric
every character in df1 same number in df2. result need:
x1 x2 x3 1 z e 2 c x y 3 b g h i know can merge 2 tables same key, in case, key on df1 data in table.
we can use match this
df1[] <- lapply(df1, function(x) df2$name[match(x, df2$id)]) df1 # x1 x2 x3 #1 z e #2 c x y #3 b g h
Comments
Post a Comment