In R, I have a tidyverse tibble called data
. There is a column of factors that I want to refactor according to some names in a vector called factor.types
, and this should be very doable as the factors originally are those as in types but with a year behind them, for example: France2019, France2020, ..., Italy2019, Italy2020, ..., ...
This is my code:
factor.types = c("France", "Italy", "Spain")
for (factor.type in factor.types) {
data[`Factor Column`] <- data %>%
pull(`Factor Column`) %>%
fct_collapse(factor.type = data %>%
pull(`Factor Column`) %>%
str_subset(pattern = factor.type)) %>%
tibble()
}
But the problem here is that the factors are now all renamed to factor.type, which is clearly not what I want. I have encountered the same problem when trying to access things like data$columnname in a loop. How do you get the literal value of your variable in the loop so that the old factors get assigned to the correct new ones?
I would really appreciate any help with this.