converting columns to factor over list of dataframes

你说的曾经没有我的故事 提交于 2021-02-05 11:15:33

问题


I'm trying to convert several columns in a list of dataframes into factors. I've tried this, but it doesn't seem to convert the columns into factors:

factor_cols_REx <- c('GESLACHT','GEVKL','BEROEP')
for (i in (1:9)) {
  dataset_RE10_2014[[i]] <- lapply(dataset_RE10_2014[[i]][factor_cols_REx],factor)
  dataset_RE10_2015[[i]] <- lapply(dataset_RE10_2015[[i]][factor_cols_REx],factor)
}

Any ideas on how to fix this?


回答1:


Let me know if I understood correctly

#DATA
dat = list(A = mtcars, B = mtcars)
#Columns we want to convert to factor
factor_cols = c("mpg", "hp")

#Go through the list using lapply and change specific columns to factor in each sub-group
#Modified from https://stackoverflow.com/a/33180265/7128934
dat2 = lapply(dat, function(x){
     x[factor_cols] = lapply(x[factor_cols], factor)
     x
    })

#Check class in output list
lapply(dat2, function(x) sapply(x, class))
#$A
#      mpg       cyl      disp        hp      drat        wt      qsec        vs        am      gear      carb 
# "factor" "numeric" "numeric"  "factor" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 

#$B
#      mpg       cyl      disp        hp      drat        wt      qsec        vs        am      gear      carb 
# "factor" "numeric" "numeric"  "factor" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 

#Check class in input list
lapply(dat, function(x) sapply(x, class))
#$A
#      mpg       cyl      disp        hp      drat        wt      qsec        vs        am      gear      carb 
#"numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 

#$B
#      mpg       cyl      disp        hp      drat        wt      qsec        vs        am      gear      carb 
#"numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 



回答2:


An approach using dplyr and purrr

library(dplyr)
library(purrr) 

factor_cols_REx <- c('GESLACHT','GEVKL','BEROEP')

dataset_RE10_2014 <- map(dataset_RE10_2014, ~mutate_at(.x, factor_cols_REx, factor))

dataset_RE10_2015 <- map(dataset_RE10_2015, ~mutate_at(.x, factor_cols_REx, factor))



回答3:


We need to have the same subset on the LHS and RHS of <-

for (i in (1:9)) {

  dataset_RE10_2014[[i]][factor_cols_REx] <- lapply(dataset_RE10_2014[[i]][factor_cols_REx], 
                            factor)
  dataset_RE10_2015[[i]][factor_cols_REx] <- lapply(dataset_RE10_2015[[i]][factor_cols_REx],
                            factor)

}


来源:https://stackoverflow.com/questions/45510024/converting-columns-to-factor-over-list-of-dataframes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!