I want to remove spaces from certain data frame variables in a for
loop. I tried something like this:
for (j in 1:5) {
df <- df %>%
dplyr::mutate(paste0("var", j) = (gsub("[[:blank:]]", "", paste0("var", j))))
}
But I got this error:
Error: unexpected '=' in:
" df <- df %>%
dplyr::mutate(paste0("var", j) ="
In reality, I have more than 5 variables, and I am planning to adjust the data using other functions, too. How can I assure that the loop elements are specific column names, so that when I do something like df$loop_element
, the variable that I want is extracted from the data frame?