I'm trying to reference variable names in for loop in R. For example, if I want to change each of the following variables to from numeric to a string
xtable<- tbl_df(cbind(x1=c(1,2,3), x2=c(3,4,5)))
for (varname in names(xtable)) {
xtable$varname<- as.character(xtable$varname)
}
or rename each variable by adding an 'a' after each variable name
for (varname in names(xtable)) {
dplyr::rename(xtable, varname = paste0(varname,'a', sep='') )
}
In general, I'm having trouble referencing the indexing variable "varname" within the for loop as the variable name it represents rather than as the word "varname".