【问题标题】:Convert one column to many using a separator [duplicate]使用分隔符将一列转换为多列[重复]
【发布时间】:2020-06-23 12:39:09
【问题描述】:

有这样的数据框:

data.frame(id = c(1,2), text = c("Google,Amazon", "Amazon,Yahoo"), stringsAsFactors = FALSE)
#   id          text
# 1  1 Google,Amazon
# 2  2  Amazon,Yahoo

如何使用逗号作为分隔符从文本列创建。预期输出示例:

data.frame(id = c(1,2), Google = c(1,0), Amazon = c(1,1), Yahoo = c(0,1))
#      id Google Amazon Yahoo
# 1     1      1      1     0
# 2     2      0      1     1

【问题讨论】:

    标签: r dataframe dplyr tidyr strsplit


    【解决方案1】:

    使用库 dplyrtidyr

    library(dplyr)
    library(tidyr)
    
    df %>% 
      mutate(
        text = strsplit(text, ","),
        value = 1
        ) %>% 
      unnest(text) %>% 
      pivot_wider(
        id_cols = id,
        names_from = text,
        values_from = value,
        values_fill = list(value = 0)
      )
    

    输出

    # A tibble: 2 x 4
    #      id Google Amazon Yahoo
    #   <dbl>  <dbl>  <dbl> <dbl>
    # 1     1      1      1     0
    # 2     2      0      1     1
    

    【讨论】:

      猜你喜欢
      • 2020-09-29
      • 1970-01-01
      • 2010-10-27
      • 2014-08-24
      • 1970-01-01
      • 2020-04-07
      • 2011-11-11
      • 2021-05-07
      相关资源
      最近更新 更多