【问题标题】:Create duplicate rows of Column A where Column B has multiple values创建 A 列的重复行,其中 B 列有多个值
【发布时间】:2022-07-11 21:28:23
【问题描述】:

我的数据框如下所示:

V1            V2 
colors1       black;yellow;green 
colors2       blue;pink;purple 

我正在尝试使用 dcast 将此 df 转换为频率矩阵: dcast(df, V2~V1) 但我需要将第二列字符串拆分为单独的值,如下所示:

V1            V2 
colors1       black
colors1       yellow
colors1       green 
colors2       blue
colors2       pink
colors2       purple 

有没有简单的方法可以做到这一点?

【问题讨论】:

标签: r


【解决方案1】:

使用tidyr 包中的separate_rows

df <- data.frame(V1=c('colors1', 'colors2'), V2=c('black;yellow;green', 'blue;pink;purple'))
  
tidyr::separate_rows(df, V2)

#> # A tibble: 6 × 2
#>   V1      V2    
#>   <chr>   <chr> 
#> 1 colors1 black 
#> 2 colors1 yellow
#> 3 colors1 green 
#> 4 colors2 blue  
#> 5 colors2 pink  
#> 6 colors2 purple

【讨论】:

    【解决方案2】:

    另一个简单的选择是像这样使用strsplit

    df <- read.table(text="V1            V2 
    colors1       black;yellow;green 
    colors2       blue;pink;purple ", header = TRUE)
    
    library(dplyr)
    library(tidyr)
    df %>% 
      mutate(V2 = strsplit(V2, ";")) %>% 
      unnest(V2)
    #> # A tibble: 6 × 2
    #>   V1      V2    
    #>   <chr>   <chr> 
    #> 1 colors1 black 
    #> 2 colors1 yellow
    #> 3 colors1 green 
    #> 4 colors2 blue  
    #> 5 colors2 pink  
    #> 6 colors2 purple
    

    reprex package (v2.0.1) 于 2022-07-11 创建

    【讨论】:

      猜你喜欢
      • 2021-06-27
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2018-06-20
      • 2016-10-25
      • 1970-01-01
      相关资源
      最近更新 更多