【问题标题】:How to make the levels of a factor in a data frame consistent across all columns?如何使数据框中的因子水平在所有列中保持一致?
【发布时间】:2015-03-29 12:56:47
【问题描述】:

我有一个包含 5 个不同列的数据框:

         Test1   Test2   Test3  Test4  Test5 
Sample1  PASS    PASS    FAIL    WARN   WARN
Sample2  PASS    PASS    FAIL    PASS   WARN
Sample3  PASS    FAIL    FAIL    PASS   WARN
Sample4  PASS    FAIL    FAIL    PASS   WARN
Sample5  PASS    WARN    FAIL    WARN   WARN

在每一列中,每个级别都分配有不同的因素。 在第 1 列中,“PASS”为 1。 在第 2 列中,“PASS”为 2,“FAIL”为 1。 在第 3 列中,“失败”为 1。 在第 4 列中,“PASS”为 1,“WARN”为 2。 在第 5 列中,“WARN”为 1。

它是按字母顺序做的 我需要“PASS”在所有列中为 1,“WARN”在所有列中为 2,“FAIL”在所有列中为 3,这样我就可以转换为矩阵并将其转换为热图。

目前,它根据在特定列中显示的因素以及按字母顺序将因素分配给级别。

如何在整个数据帧中保持不变?

【问题讨论】:

    标签: r matrix dataframe r-factor


    【解决方案1】:

    您可以通过循环 (lapply) 将数据集“df”的级别更改为相同的顺序,然后使用指定的 levels 再次转换为 factor 并将其分配回相应的列。

    lvls <- c('PASS', 'WARN', 'FAIL')
    df[] <-  lapply(df, factor, levels=lvls)
    str(df)
    # 'data.frame': 5 obs. of  5 variables:
    # $ Test1: Factor w/ 3 levels "PASS","WARN",..: 1 1 1 1 1
    # $ Test2: Factor w/ 3 levels "PASS","WARN",..: 1 1 3 3 2
    # $ Test3: Factor w/ 3 levels "PASS","WARN",..: 3 3 3 3 3
    # $ Test4: Factor w/ 3 levels "PASS","WARN",..: 2 1 1 1 2
    # $ Test5: Factor w/ 3 levels "PASS","WARN",..: 2 2 2 2 2
    

    如果您选择使用data.table

    library(data.table)
    setDT(df)[, names(df):= lapply(.SD, factor, levels=lvls)]
    

    setDT 将“data.frame”转换为“data.table”,将数据集的列名(:=)分配给重新转换的因子列(lapply(..))。 .SD 表示“数据表的子集”。

    数据

    df <- structure(list(Test1 = structure(c(1L, 1L, 1L, 1L, 1L), 
    .Label = "PASS", class = "factor"), 
      Test2 = structure(c(2L, 2L, 1L, 1L, 3L), .Label = c("FAIL", 
     "PASS", "WARN"), class = "factor"), Test3 = structure(c(1L, 
     1L, 1L, 1L, 1L), .Label = "FAIL", class = "factor"), Test4 = 
     structure(c(2L, 1L, 1L, 1L, 2L), .Label = c("PASS", "WARN", "FAIL"), 
     class = "factor"), Test5 = structure(c(1L, 1L, 1L, 1L, 1L), .Label = 
    "WARN", class = "factor")), .Names = c("Test1", 
    "Test2", "Test3", "Test4", "Test5"), row.names = c("Sample1", 
    "Sample2", "Sample3", "Sample4", "Sample5"), class = "data.frame")
    

    【讨论】:

      【解决方案2】:

      一种更通用的方法,假设您的 data.frameNA 中可以有其他 string 值:

      library(magrittr)
      
      fac = df %>% as.matrix %>% as.vector %>% unique
      df1 = data.frame(lapply(df, factor, levels = fac[!is.na(fac)]))
      

      【讨论】:

        【解决方案3】:

        使用dplyr

        library(dplyr)
        df <- df %>% mutate_each(funs(factor(., levels = c('PASS', 'WARN', 'FAIL'))))
        

        你得到:

        #> str(df)
        #'data.frame':  5 obs. of  5 variables:
        # $ Test1: Factor w/ 3 levels "PASS","WARN",..: 1 1 1 1 1
        # $ Test2: Factor w/ 3 levels "PASS","WARN",..: 1 1 3 3 2
        # $ Test3: Factor w/ 3 levels "PASS","WARN",..: 3 3 3 3 3
        # $ Test4: Factor w/ 3 levels "PASS","WARN",..: 2 1 1 1 2
        # $ Test5: Factor w/ 3 levels "PASS","WARN",..: 2 2 2 2 2
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-12-21
          • 1970-01-01
          • 2017-02-19
          • 1970-01-01
          • 2021-08-17
          相关资源
          最近更新 更多