【问题标题】:Make a string after grouping分组后做一个字符串
【发布时间】:2020-08-07 18:48:38
【问题描述】:

这是我的问题。我有关于城市代码 (GeoCode) 和邮政编码 (PostCode) 的数据。通常几个邮政编码对应一个城市代码。如果是这样的话,我想用一串对应同一个城市的邮政编码来做一列:

ID<-1:10
GeoCode<-c("AA","BB","BB","CC","CC","CC","DD","DD","DD","DD")
PostCode<-c("01","10","11","20","21","22","30","31","32","33")
data<-data.frame(ID,GeoCode,PostCode)

我想做这样的桌子。例如“20_21_22”属于城市代码 CC

   ID GeoCode PostCode strPostcode
1   1      AA       01          01
2   2      BB       10       10_11
3   3      BB       11       10_11
4   4      CC       20    20_21_22
5   5      CC       21    20_21_22
6   6      CC       22    20_21_22
7   7      DD       30 30_31_32_33
8   8      DD       31 30_31_32_33
9   9      DD       32 30_31_32_33
10 10      DD       33 30_31_32_33

【问题讨论】:

    标签: r string dplyr


    【解决方案1】:

    我们可以按 'GeoCode' 和 paste 分组 unique 'PostCode' 中的所有 mutate

    library(dplyr)
    library(stringr)
    data %>% 
        group_by(GeoCode) %>%
        mutate(strPostcode = str_c(unique(PostCode), collapse="_"))
    # A tibble: 10 x 4
    # Groups:   GeoCode [4]
    #      ID GeoCode PostCode strPostcode
    #   <int> <chr>   <chr>    <chr>      
    # 1     1 AA      01       01         
    # 2     2 BB      10       10_11      
    # 3     3 BB      11       10_11      
    # 4     4 CC      20       20_21_22   
    # 5     5 CC      21       20_21_22   
    # 6     6 CC      22       20_21_22   
    # 7     7 DD      30       30_31_32_33
    # 8     8 DD      31       30_31_32_33
    # 9     9 DD      32       30_31_32_33
    #10    10 DD      33       30_31_32_33
    

    或者base R的选项

    data$strPostcode <- with(data, ave(PostCode, GeoCode, FUN = 
            function(x) paste(unique(x), collapse="_")))
    

    【讨论】:

      【解决方案2】:

      @akrun 的带有ave 的基本 R 选项是有效的。这是另一种解决方法

      merge(data,
        aggregate(PostCode ~ ., data[-1], paste0, collapse = "_"),
        by = "GeoCode",
        all = TRUE
      )
      

      给了

         GeoCode ID PostCode.x  PostCode.y
      1       AA  1         01          01
      2       BB  2         10       10_11
      3       BB  3         11       10_11
      4       CC  4         20    20_21_22
      5       CC  5         21    20_21_22
      6       CC  6         22    20_21_22
      7       DD  7         30 30_31_32_33
      8       DD  8         31 30_31_32_33
      9       DD  9         32 30_31_32_33
      10      DD 10         33 30_31_32_33
      

      【讨论】:

        【解决方案3】:

        或者你可以试试这个

        data2 <- data %>% 
          group_by(GeoCode) %>% 
          mutate(strPostCode = paste0(unique(PostCode), collapse = "_"))
        # ID  GeoCode PostCode strPostCode
        # <int> <chr>   <chr>    <chr>      
        # 1     1 AA      01       01         
        # 2     2 BB      10       10_11      
        # 3     3 BB      11       10_11      
        # 4     4 CC      20       20_21_22   
        # 5     5 CC      21       20_21_22   
        # 6     6 CC      22       20_21_22   
        # 7     7 DD      30       30_31_32_33
        # 8     8 DD      31       30_31_32_33
        # 9     9 DD      32       30_31_32_33
        # 10    10 DD     33       30_31_32_33
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-04-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-13
          • 2021-06-27
          相关资源
          最近更新 更多