I have the below df
var1 var2 a1 a2
1 a b y z
2 b a z y
3 b b z z
created from the following code
help <- data.frame(var1 = c("a", "b", "b"), var2 = c("b", "a", "b"), a1 = c(y, z, z), a2 = c(z, y, z))
my intention is to create an ifelse statement where I am able to (1) replace all a1 values to 'bp' when var1 equals 'a', and (2) replace all a2 values to 'bp' when var2 equals 'a'. I do not want to change the value if var1 or var2 is not 'a'.
Is the ifelse command not the best way to solve this problem? I started doing each one manually using help$a1[help$a1 == "a"] <- "bp" however, this will take a bit of time as I have multiple variables and a large dataset. Any assistance would be great.
Many thanks.
var1.rules <- c(a=0,b=1,c=2)andvar2.rules <- c(a=0,b=1,c=2)- Marat Talipovc('var1', 'var2', 'var3',...,'varN', 'a1', a2', 'a3', ..., 'aN'), and you need to set value in columnvarXto 'bp' if the corresponding row in the 'aX' column is equal to 'a' for any X from 1 to N? - Marat Talipov