【问题标题】:One-hot-encoding a R list of charactersOne-hot-encoding R 字符列表
【发布时间】:2020-10-13 15:43:27
【问题描述】:

我有以下 R 数据框:

id    color
001   blue
001   yellow
001   red
002   blue
003   blue
003   yellow

将这样的数据帧单热编码为以下内容的一般方法是什么:

id    blue    yellow    red
001   1       1         1
002   1       0         0
003   1       0         1

非常感谢。

【问题讨论】:

    标签: r dataframe one-hot-encoding


    【解决方案1】:

    试试这个。您可以为数据中存在的那些观察值创建一个变量等于 1,然后使用 pivot_wider() 重塑这些值。由于您将获得 NA 用于数据中不存在的类,因此您可以使用 replace() 将其替换为零。这里使用tidyverse函数的代码:

    library(dplyr)
    library(tidyr)
    #Code
    dfnew <- df %>% mutate(val=1) %>%
      pivot_wider(names_from = color,values_from=val) %>%
      replace(is.na(.),0)
    

    输出:

    # A tibble: 3 x 4
         id  blue yellow   red
      <int> <dbl>  <dbl> <dbl>
    1     1     1      1     1
    2     2     1      0     0
    3     3     1      1     0
    

    使用的一些数据:

    #Data
    df <- structure(list(id = c(1L, 1L, 1L, 2L, 3L, 3L), color = c("blue", 
    "yellow", "red", "blue", "blue", "yellow")), class = "data.frame", row.names = c(NA,-6L))
    

    【讨论】:

    • 需要加载ggplot2,dplyr,readr,purrr,tibble,stringr,forcats吗?
    • @sindri_baldur 不是真的,如果你觉得舒服我只能添加特定的包!
    【解决方案2】:

    在 R 中有很多方法可以做到这一点。这取决于您使用的软件包。大多数建模包,如carettidymodels 都具有为您执行此操作的功能。

    但是,如果您不使用建模包,tidyverse 有一个简单的方法来做到这一点。

    library(dplyr)
    library(tidyr)
    
    df <- tribble(
      ~id,    ~color,
      '001',   'blue',
      '001',   'yellow',
      '001',   'red',
      '002',   'blue',
      '003',   'blue',
      '003',   'yellow')
    
    df_onehot <- df %>%
      mutate(value = 1) %>%
      pivot_wider(names_from = color,values_from = value,values_fill = 0)
    # A tibble: 3 x 4
    #    id     blue yellow   red
    #   <chr> <dbl>  <dbl> <dbl>
    # 1 001       1      1     1
    # 2 002       1      0     0
    # 3 003       1      1     0
    

    【讨论】:

    • 你可以避免加载dplyr并使用df$value &lt;- 1
    • dplyrtidyr 的依赖项,因此它已经被导入。我把它放在那里是为了具体。此外,mutate 使该过程易于阅读,并将其全部保存在一个链中。
    • 我明白了,但至少不会污染命名空间library(tidyr);mutate(iris, test = "bla") # could not find function "mutate"
    【解决方案3】:

    data.table:

    library(data.table)
    dcast(setDT(df), id ~ color, fun.aggregate = length)
    
    #     id blue red yellow
    # 1: 001    1   1      1
    # 2: 002    1   0      0
    # 3: 003    1   0      1
    

    tidyr的逻辑相同:

    library(tidyr)
    pivot_wider(df, names_from=color, values_from=color, values_fn=length, values_fill=0)
    
    #   id     blue yellow   red
    #   <chr> <int>  <int> <int>
    # 1 001       1      1     1
    # 2 002       1      0     0
    # 3 003       1      1     0
    

    Base R:

    out <- as.data.frame.matrix(pmin(with(df, table(id, color)), 1))
    out$id <- rownames(out)
    out
    #     blue red yellow  id
    # 001    1   1      1 001
    # 002    1   0      0 002
    # 003    1   0      1 003
    

    可重复的数据

    df <- data.frame(
      id = c("001", "001", "001", "002", "003", "003"), 
      color = c("blue", "yellow", "red", "blue", "blue", "yellow")
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      相关资源
      最近更新 更多