代码之家  ›  专栏  ›  技术社区  ›  s__

R:除了一个单元格,有唯一的行,如何得到保持差异的唯一行

r
  •  0
  • s__  · 技术社区  · 7 年前

    我正在处理以下数据:

    df <- data.frame(
          point = c('a','b','b','c'),
          value =c(1,2,2,3),
          x_p2=c(5,6,6,7),
          y_p2 =c(3,4,4,3),
          date =c(1,4,4,7),
          variable =c(4,3,3,1),
         other =c('x','zz','zk','x')
                    )
    
    > df
      point value x_p2 y_p2 date variable other
    1     a     1    5    3    1        4     x
    2     b     2    6    4    4        3    zz
    3     b     2    6    4    4        3    zk
    4     c     3    7    3    7        1     x
    

    正如您所看到的,每一行都是唯一的,除了第二行和第三行之外,只有在 other 列。
    我想要的是有一个独一无二的结果,但是合并了一些不常见的结果:更清楚地说,我想要这个结果:

      point value x_p2 y_p2 date variable other
    1     a     1    5    3    1        4     x
    2     b     2    6    4    4        3    zz/zk
    3     c     3    7    3    7        1     x
    

    我试过了 unique() 函数,但显然它需要第一个不相等的行,并且我的行在每一列中都是不同的,而且它不会“熔化”另一个不同的字段,我希望保持这一点。
    我不知道该如何理解(诚实地说,这个问题的标题也是如此)。你有什么建议?事先谢谢。

    3 回复  |  直到 7 年前
        1
  •  2
  •   BENY    7 年前

    通过使用 dplyr

    df%>%group_by( point,value,x_p2,y_p2,date,variable)%>%dplyr::summarise(other=paste(other,collapse='/'))
    # A tibble: 3 x 7
    # Groups:   point, value, x_p2, y_p2, date [?]
       point value  x_p2  y_p2  date variable other
      <fctr> <dbl> <dbl> <dbl> <dbl>    <dbl> <chr>
    1      a     1     5     3     1        4     x
    2      b     2     6     4     4        3 zz/zk
    3      c     3     7     3     7        1     x
    
        2
  •  1
  •   Onyambu    7 年前
     aggregate(df,list(do.call(paste,df[-7])),function(x)unique(x))[-1]
      point value x_p2 y_p2 date variable  other
    1     a     1    5    3    1        4      x
    2     b     2    6    4    4        3 zz, zk
    3     c     3    7    3    7        1      x
    
        3
  •  1
  •   akrun    7 年前

    这是一个 base R 选项 merge unique . 获取 独特的 删除最后一列的行,以及 合并 paste Ed“其他” aggregated 按“点”

    merge(unique(df[-ncol(df)]), aggregate(other ~ point, df, paste, collapse="/"))
    #  point value x_p2 y_p2 date variable other
    #1     a     1    5    3    1        4     x
    #2     b     2    6    4    4        3 zz/zk
    #3     c     3    7    3    7        1     x
    

    如果我们想保持 list 列,这可以通过 summarise

    library(tidyverse)
    df %>% 
        group_by_at(vars(names(.)[1:6])) %>% 
        summarise(other = list(other))
    

    或与 aggregate

    aggregate(other ~ ., df, I)