【问题标题】:R determine if more than 1 value exists in a column, mutate concatenate if trueR确定列中是否存在多个值,如果为真则突变连接
【发布时间】:2020-09-14 15:36:41
【问题描述】:

无法完全找到解决此问题的方法,正在寻找一些指导。

这是我拥有的数据集的示例:

     ID   Rank    Date       Date2.      Group   Group2
1   5678   1    2000-01-01   2010-05-02    A      A
2   5678   2    2010-05-02   2010-05-02    A      A
3   1234   1    2000-01-01   2015-06-03    B      A&B
4   1234   2    2015-06-03   2015-06-03    A      A&B

我想按 ID 进行分组,并确定“组”列中该 ID 是否存在多个值。如果是这样,请将它们连接在一起。

Group2 是基于分组 ID 并查看 Group 中是否存在多个值的所需输出。我正在使用 dplyr,但不确定从这里去哪里:

df <- df %>% group_by(ID) %>% mutate(Group2 = if else(?))

【问题讨论】:

    标签: r group-by string-concatenation dplyr


    【解决方案1】:

    我认为最简单的方法是unique 函数。此函数使用给定向量的不同值创建一个向量。

    > df %>%
       group_by(ID)%>%
       mutate(Group2=paste(sort(unique(Group)),
                           collapse="&"))
    
    # A tibble: 4 x 3
    # Groups:   ID [2]
         ID Group Group2
      <dbl> <chr> <chr> 
    1  5678 A     A     
    2  5678 A     A     
    3  1234 B     A&B   
    4  1234 A     A&B   
    

    【讨论】:

      【解决方案2】:

      data.table 的选项

      library(data.table)
      setDT(df)[, Group2 := paste(sort(unique(Group)), collapse="&"), ID]
      

      数据

      df <- structure(list(ID = c(5678L, 5678L, 1234L, 1234L), Rank = c(1L, 
      2L, 1L, 2L), Date = c("2000-01-01", "2010-05-02", "2000-01-01", 
      "2015-06-03"), Date2. = c("2010-05-02", "2010-05-02", "2015-06-03", 
      "2015-06-03"), Group = c("A", "A", "B", "A")), row.names = c("1", 
      "2", "3", "4"), class = "data.frame")
      

      【讨论】:

        【解决方案3】:

        我建议下一个方法:

        library(dplyr)
        #Code
        df %>% group_by(ID) %>%
          arrange(ID,Group) %>%
          #Identify unique elements
          mutate(NUnique=n_distinct(Group)) %>%
          mutate(Group2=ifelse(NUnique>1,paste0(Group,collapse = '&'),Group))
        

        输出:

        # A tibble: 4 x 7
        # Groups:   ID [2]
             ID  Rank Date       Date2.     Group NUnique Group2
          <int> <int> <chr>      <chr>      <chr>   <int> <chr> 
        1  1234     2 2015-06-03 2015-06-03 A           2 A&B   
        2  1234     1 2000-01-01 2015-06-03 B           2 A&B   
        3  5678     1 2000-01-01 2010-05-02 A           1 A     
        4  5678     2 2010-05-02 2010-05-02 A           1 A  
        

        使用的一些数据:

        #Data
        df <- structure(list(ID = c(5678L, 5678L, 1234L, 1234L), Rank = c(1L, 
        2L, 1L, 2L), Date = c("2000-01-01", "2010-05-02", "2000-01-01", 
        "2015-06-03"), Date2. = c("2010-05-02", "2010-05-02", "2015-06-03", 
        "2015-06-03"), Group = c("A", "A", "B", "A")), row.names = c("1", 
        "2", "3", "4"), class = "data.frame")
        

        【讨论】:

          【解决方案4】:

          基本 R 选项

          within(df,Group2 <- ave(Group,ID,FUN = function(x) ifelse(length(unique(x))==1,x,paste0(x,collapse = "&"))))
          

          给予

              ID Rank       Date     Date2. Group Group2
          1 5678    1 2000-01-01 2010-05-02     A      A
          2 5678    2 2010-05-02 2010-05-02     A      A
          3 1234    1 2000-01-01 2015-06-03     B    B&A
          4 1234    2 2015-06-03 2015-06-03     A    B&A
          

          【讨论】:

            【解决方案5】:

            继续使用您的代码,我会尝试:

            df %>%
                group_by(ID) %>%
                mutate(Group2 = ifelse(n_distinct(Group) == 1, as.character(Group), paste0(sort(Group), collapse = "&")))
            
            # A tibble: 4 x 6
            # Groups:   ID [2]
                 ID  Rank Date       Date2.     Group Group2
              <int> <int> <fct>      <fct>      <fct> <chr> 
            1  5678     1 2000-01-01 2010-05-02 A     A     
            2  5678     2 2010-05-02 2010-05-02 A     A     
            3  1234     1 2000-01-01 2015-06-03 B     A&B   
            4  1234     2 2015-06-03 2015-06-03 A     A&B   
            

            【讨论】:

            • 谢谢!我确实收到此代码错误Error in UseMethod("group_by_") : no applicable method for 'group_by_' applied to an object of class "c('integer', 'numeric')"
            • 是的!我更新了我的答案。错误来自 Group 一个因素
            • 请注意,现在我使用ifelse 而不是if_else
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-09-27
            • 2021-01-16
            • 1970-01-01
            • 1970-01-01
            • 2011-03-12
            • 1970-01-01
            相关资源
            最近更新 更多