this is my first post in stackoverflow and english is not my first language, so I'll apologize in advance for any mistakes both in grammar and programming.
I need to replace values in one column of my data frame based on part of values which are in another data frame. My question is similar to this post here, but in their example they have all the possible errors mapped out. In my case, I only need a part of the string to know if I need to replace a value or not.
I already tried to use "if_else" and "grepl" with dplyr. "Grepl" works as long as I have only one row on the second dataframe, when I insert another example I get an error.
Right now my real DF has around 30k rows and 33 variables, and the second DF with the right values may grow every month, so I'm trying to run away from loops as much as I can.
I made a mock table with random data to simulate my need:
library(dplyr)
df1 <- data.frame(Supplier = c("AAA","CCC","CCE","DDD","EEE","EED","GGG","HHH","III","JJJ"),
Value = c(100,200,300,400,200, 100,200,40,150,70))
df2 <- data.frame(Supplier =c("CC","EE","GG"),
New_Supplier = c("Red","Blue","Green"))
#Example 1: Unfortunately this Won't work unless I have an exact match:
df1$Supplier <- if_else(df1$Supplier %in% df2$Supplier, df2$New_Supplier, df1$Supplier)
# Example 2: Only works if I have one example:
df1$Supplier <- if_else(grepl(df2$Supplier, df1$Supplier), df2$New_Supplier, df1$Supplier)
So I have this on the first data frame:
Supplier Value
1 AAA 100
2 CCC 200
3 CCE 300
4 DDD 400
5 EEE 200
6 EED 100
7 GGG 200
8 HHH 40
9 III 150
10 JJJ 70
And this on the second data frame:
Supplier New_Supplier
1 CC Red
2 EE Blue
3 GG Green
My end goal is to have something like this:
Supplier Value
1 AAA 100
2 Red 200
3 Red 300
4 DDD 400
5 Blue 200
6 Blue 100
7 Green 200
8 HHH 40
9 III 150
10 JJJ 70
Thanks in advance!