将列转换为数据帧列表的因子

时间:2017-08-04 15:01:09

标签: r list dataframe lapply r-factor

我试图将数据框列表中的多个列转换为因子。 我试过这个,但似乎没有将列转换成因素:

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)
}

有关如何解决此问题的任何想法?

3 个答案:

答案 0 :(得分:3)

如果我理解正确,请告诉我

#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" 

答案 1 :(得分:2)

使用dplyrpurrr

的方法
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))

答案 2 :(得分:1)

我们需要在<-

的LHS和RHS上使用相同的子集
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)

}