How to output duplicated rows

前端 未结 6 1730
情话喂你
情话喂你 2020-12-04 03:39

I have the following data:

x1  x2  x3  x4
34  14  45  53 
2   8   18  17
34  14  45  20
19  78  21  48 
2   8   18  5

In rows 1 and 3; and

6条回答
  •  广开言路
    2020-12-04 04:17

    There is another way to solve both questions using two packages.

    library(DescTools)
    library(dplyr)
    dat[AllDuplicated(dat[1:3]), ] %>% # this line is to find duplicates
      group_by(x1, x2) %>% # the lines followed are to sum up
      mutate(x4 = sum(x4)) %>%
      unique()
    # Source: local data frame [2 x 4]
    # Groups: x1, x2
    # 
    #   x1 x2 x3 x4
    # 1 34 14 45 73
    # 2  2  8 18 22
    

提交回复
热议问题