I want to make 5 commands on list of variables. In my real data I have a lot more but for the purpose of this example, say I have two. What I want to do - written out - is this:
data$inst <- tolower(data$inst)
data$inst <- str_replace(data$inst, "è|ê", "e")
data$inst <- str_replace(data$inst, "à|â", "a")
data$inst <- str_replace(data$inst, "’|`", " ")
data$inst <- str_replace(data$inst, " ", " ")
data$inst_city <- tolower(data$inst_city)
data$inst_city <- str_replace(data$inst_city, "è|ê", "e")
data$inst_city <- str_replace(data$inst_city, "à|â", "a")
data$inst_city <- str_replace(data$inst_city, "’|`", " ")
data$inst_city <- str_replace(data$inst_city, " ", " ")
I am sure they must be a smart (read: loop) way of doing this, but this does not manipulate the variables, in the way I want:
list <- c('data$inst', 'data$inst_city')
for (var in list) {
var <- tolower(var)
var <- str_replace(var, "è|ê", "e")
var <- str_replace(var, "à|â", "a")
var <- str_replace(var, "’|`", " ")
var <- str_replace(var, " ", " ")
}
So how do I make a loop on the variables inst
and inst_city
in data
?
As a side note, I am fairly new to R and suspect this is a situation where the pipe operator would be handy, but can't quite figure out how.
EDIT: I can see that my question has been closed, because there are questions answered already on how to replace the contents of accented letters with non-accented letters. Thanks for providing the references. For the purposes of learning R, however, I am still very interested in learning how to loop over variables, or how to solve my problem using a loop. This is because I am very interested learning in how looping works in R compared to Stata.