Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

replace strings in a column with their equivalents in another column in another dataframe R

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading