I'm trying to use apply (and avoid a for loop) to run the pair of operations below on a dataframe using values from two different strings. The strings and the dataframe I'm working with look like this:
x <- c("A", "B", "C")
y <- c(1, 3, 4)
df:
var1 var2
ddAd NA
dBdd NA
ddCd NA
I'm trying to run the following two lines of code for each pair of values in the two strings using apply.
z <- grep(x, df$var1, value = FALSE)
df[z, 3] <- y
The end result I'm going for is this:
var1 var2
ddAd 1
dBdd 3
ddCd 4
My attempts to use apply so far seem to work fine with the first line of code, but I run into trouble with the second line. I think I need to run an apply command within an apply command in this situation, but I haven't been able to get that to work. Can anyone show me how to use "apply" in this situation? Thanks!