0
votes

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.

1
How many rules for replacement do you have? My wild guess is that supplementary data sets with rules might be of help in your case, e.g. var1.rules <- c(a=0,b=1,c=2) and var2.rules <- c(a=0,b=1,c=2) - Marat Talipov
I had to edit the post slightly due to needing all character vectors. I have 60 different unique variables that need to be changed within var1 and var2... but not sure if that answers your question... - b222
You mean that your column names look like c('var1', 'var2', 'var3',...,'varN', 'a1', a2', 'a3', ..., 'aN'), and you need to set value in column varX to 'bp' if the corresponding row in the 'aX' column is equal to 'a' for any X from 1 to N? - Marat Talipov
not sure i follow the question, but the answer below by A Webb did work... if that helps clarify? - b222

1 Answers

1
votes

You can use standard subsetting. Assuming per question edit that a1 and a2 are of class "character", then

df
#  var1 var2 a1 a2
#1    a    b  y  z
#2    b    a  z  y
#3    b    d  z  z

df[,3:4][df[,1:2]=="a"]<-"bp"

df
#  var1 var2 a1 b2
#1    a    b bp  z
#2    b    a  z bp
#3    b    d  z  z

You can also take advantage of your naming convention for the same

sel<-paste0("var",1:2)
mut<-paste0("a",1:2)
df[,mut][df[,sel]=="a"]<-"bp"

In either case the inner part, e.g. df[,sel]=="a" is returning a logical matrix the same shape as df[,sel] (and hence the same shape as df[,mut]), which is TRUE where and only where there was an "a". The outer part, e.g. df[,mut][...], uses that logical matrix to indicate the subset subject to the assignment.