0

I have two different data frames. For example Lets say dataframe 1:

| A |
| -------- | 
| 11  | 
| 24  | 
| 3|
| 14| 
| 17|  

and dataframe 2:

|b | c |
| -------- | -------------- |
| 23   | Cat           |
| 24   | Dog           |
| 11   | Cow           |
| 3  | Snake            |

I want to get c values to dataframe 1. So my desired outcome would be something like that: and dataframe2:

|a | new value|
| -------- | -------------- |
|11   | Cow           |
| 24   | Dog           |
| 3   | Snake          |
| 14  | NA            |
| 17  | NA            |

I tried this:

library(dplyr)
dataframe1<-dataframe1 %>% mutate(newvalue=ifelse(a%in% dataframe2$b, dataframe2$c,NA))

But this doesnt work properly. What could I do?

harre
  • 7,081
  • 2
  • 16
  • 28
Lars
  • 1
  • 1
    You want to do a left join by A and b. See: https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right – harre Jul 20 '22 at 13:57
  • Use `dataframe1 %>% left_join(dataframe2, by=c("A"="b")` – MrFlick Jul 20 '22 at 14:12

0 Answers0