Home > Net >  replace strings in a column with their equivalents in another column in another dataframe R
replace strings in a column with their equivalents in another column in another dataframe R

Time:11-09

consider two dataframes

df1 <- data.frame(a=LETTERS[1:6],
                  b=c("apple", "apple","dog", "red", "red","red"))
df2 <- data.frame(col1=c("apple", "golf", "dog", "red"),
                  col2=c("fruit", "sport","animal", "color"))
> df1
  a     b
1 A apple
2 B apple
3 C   dog
4 D   red
5 E   red
6 F   red

> df2
   col1   col2
1 apple  fruit
2  golf  sport
3   dog animal
4   red  color

I want to create

> output
  a      b
1 A  fruit
2 B  fruit
3 C animal
4 D  color
5 E  color
6 F  color

I get the output I am looking for using the basic for loop. But is there any neat nice way to get this through pipes of dplyr?

for(i in 1:nrow(df1)){
    df1[i,2] <- df2[df2$col1==df1[i,2], 2]
}

CodePudding user response:

Use a join

library(dplyr)
left_join(df1, df2, by = c("b" = "col1")) %>%
   select(a, b = col2)

-output

a      b
1 A  fruit
2 B  fruit
3 C animal
4 D  color
5 E  color
6 F  color

Or in base R with match or named vector

df1$b <- setNames(df2$col2, df2$col1)[df1$b]

CodePudding user response:

A solution with lapply and match:

df1$b  <-  unlist(lapply(df1$b, function(x) df2$col2[match(x, df2$col1)]))
df1
 a      b
1 A  fruit
2 B  fruit
3 C animal
4 D  color
5 E  color
6 F  color

CodePudding user response:

df1$b <- df2[ df2$col1 == df1$b, 'col2' ]
  • Related