【问题标题】:pivot_wider: How to add rows for non unique values?pivot_wider:如何为非唯一值添加行?
【发布时间】:2021-04-21 20:34:01
【问题描述】:

我有一个长格式的表格,其中包含不同语言的物种名称和 2 级 id 编号,一个对应于该物种,另一个对应于该物种的不同名称。

例如:

species_list <- cbind.data.frame(num_sp= c(100,101,101,101,101,102,102,103), num_name = c(1,1,2,3,4,1,2,1), language=c("latin","latin", "english", "english", "english","latin","english","english"), name=c("marinus thingus", "aquaticus stuffae", "blob","water being","marine creature","altermarinus stuffae","other marine stuff", "unknown thingy"))

  num_sp num_name language                 name
1    100        1    latin      marinus thingus
2    101        1    latin    aquaticus stuffae
3    101        2  english                 blob
4    101        3  english          water being
5    101        4  english      marine creature
6    102        1    latin altermarinus stuffae
7    102        2  english   other marine stuff
8    103        1  english       unknown thingy

我想要一个对应表,以便能够从英文名称中获取拉丁名称。我认为该表应该给我列中的语言,并且只有行中的 num_sp id,这对应于 pivot_wider,除了我的“num_sp”不是唯一标识符,因此该函数给出警告:

Warning message:
Values are not uniquely identified; output will contain list-cols.

有没有办法(可能是通过 values_fn?)将这些列表列拆分为不同的行,从而获得这样的表格:

 num_sp            english                latin
1    100               <NA>      marinus thingus
2    101               blob    aquaticus stuffae
3    101        water being    aquaticus stuffae
4    101    marine creature    aquaticus stuffae
5    102 other marine stuff altermarinus stuffae
6    103     unknown thingy                 <NA>

感谢您的宝贵时间!

【问题讨论】:

    标签: r duplicates tidyr


    【解决方案1】:

    这将解决问题

    species_list %>% pivot_wider(id_cols = num_sp, 
                                 names_from = language, 
                                 values_from = name, 
                                 values_fn = list) %>%
      unnest(-num_sp)
    
    # A tibble: 6 x 3
      num_sp latin                english           
       <dbl> <chr>                <chr>             
    1    100 marinus thingus      NA                
    2    101 aquaticus stuffae    blob              
    3    101 aquaticus stuffae    water being       
    4    101 aquaticus stuffae    marine creature   
    5    102 altermarinus stuffae other marine stuff
    6    103 NA                   unknown thingy    
    

    【讨论】:

    • 当然!我知道有一个非常简单的答案。非常感谢!
    • 将您的解决方案用于我的真实数据集时,我收到此错误消息:不兼容的长度:3、2。我猜这是因为在数据中的某个点,其中一种语言有两个版本和另一个有三个?你知道怎么处理吗?
    • 这可能不是问题?更仔细地查看以找出与样本数据集的差异
    【解决方案2】:

    您可以重新编号num_name 列,将数据转换为宽,fill 转换为值。

    library(dplyr)
    library(tidyr)
    
    species_list %>%
      group_by(num_sp, language) %>%
      mutate(num_name = row_number()) %>%
      pivot_wider(names_from = language, values_from = name) %>%
      fill(latin, english) %>%
      ungroup
      
    
    #  num_sp num_name latin                english           
    #   <dbl>    <int> <chr>                <chr>             
    #1    100        1 marinus thingus      NA                
    #2    101        1 aquaticus stuffae    blob              
    #3    101        2 aquaticus stuffae    water being       
    #4    101        3 aquaticus stuffae    marine creature   
    #5    102        1 altermarinus stuffae other marine stuff
    #6    103        1 NA                   unknown thingy    
    

    【讨论】:

    • 谢谢!我已经多次看到“row_number”解决方案,但由于我已经有 2 个 id 列并且没有人完成这项工作,我认为这不会奏效。我找不到要放入“group_by”的变量的正确组合。而且我也不知道“填充”太棒了!
    猜你喜欢
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    相关资源
    最近更新 更多