【问题标题】:converting columns to factor over list of dataframes将列转换为数据框列表的因子
【发布时间】:2018-01-12 13:58:50
【问题描述】:

我正在尝试将数据框列表中的几列转换为因子。 我已经尝试过了,但它似乎没有将列转换为因子:

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

关于如何解决这个问题的任何想法?

【问题讨论】:

  • 你能解释得更清楚吗?
  • 感谢您的解决方案,它们都有效!

标签: r list dataframe lapply r-factor


【解决方案1】:

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

#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】:

    使用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))
    

    【讨论】:

      【解决方案3】:

      我们需要在&lt;- 的 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)
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-15
        • 2014-01-05
        相关资源
        最近更新 更多