简体   繁体   中英

Use a list of variables and levels to convert character data to factors using dplyr

I'm working with survey data and I need to code the response values as factors (eg Strongly disagree, disagree, agree, Strongly agree). Different questions have different response options and need to be coded appropriately. I have an excel file that lists every question and the ordered response options. I've written a for loop to convert all the variables, but would like to understand how to do it with purrr or dplyr syntax.

Here is a simple example:

library(tidyverse)

dat <- iris %>% 
  mutate(
    Species = as.character(Species),
    second_var = as.character(round(Sepal.Length)))

factor_map <- data.frame(
  var = c("Species", "second_var"), 
  response_opts = c("setosa,versicolor,virginica", 
               "4,5,6,7,8")) 

# convert character string of options into lists
factor_map2 <- factor_map %>% 
  mutate(levels = str_split(response_opts, ","))

# simple for loop                  
dat2 <- dat
for (i in 1:nrow(factor_map2)) {
  v <- factor_map2$var[i]
  l <- factor_map2$levels[[i]]
  dat2[[v]] = factor(dat2[[v]], levels = l)
  rm(v, l)
}

# How to use factor_map to convert the columns in dat to factors? 

# map2 doesn't seem to work, unclear why it says .x has length of 6
dat %>% 
  map2(factor_map2$var, factor_map2$levels,
       function(x, y) factor(x, levels = y))

# Can I pass a vector of variable specific levels into across?
dat %>% 
  mutate(across(factor_map2$var, factor, # somehow pass in the levels

It can be

map2_dfc(factor_map2$var, factor_map2$levels, 
      ~ factor(dat[[.x]], levels = .y))%>%
   setNames(factor_map2$var)

Or another option without using any new package ie with only dplyr is

dat %>%
     mutate(across(all_of(factor_map2$var), ~ factor(., levels = 
           factor_map2$levels[match(cur_column(), factor_map2$var)])))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM