R 如何合并某些变量中相同的行以及其他变量的值之和

R 如何合并某些变量中相同的行以及其他变量的值之和,r,dplyr,R,Dplyr,我有一个数据库,其中包含文本变量和用于定性分析的代码。每当应用代码时都会生成每一行,这意味着如果一个句子应用了3个代码,那么数据库将有三行代码。我想合并它,保留其余变量的数据,并对代码变量求和 我一直在寻找如何做到这一点,但找不到办法 example<-tibble(segments=c('Brexit is bad','Brexit is bad','We need a sit on the table','We need a sit on the table'), actor=c

我有一个数据库,其中包含文本变量和用于定性分析的代码。每当应用代码时都会生成每一行,这意味着如果一个句子应用了3个代码,那么数据库将有三行代码。我想合并它,保留其余变量的数据,并对代码变量求和

我一直在寻找如何做到这一点,但找不到办法

example<-tibble(segments=c('Brexit is bad','Brexit is bad','We need a sit on the table','We need a sit on the table'),
   actor=c("SNP", "SNP", "Labour", "Labour"),
   year=c(2015, 2015, 2017,2017),
   TL_Brexit=c(1,0,0,0),
   Bre_negative=c(0,1,0,0),
   TL_participation=c(0,0,1,0),
   TD_other=c(0,0,0,1))
您可以看到,有两个引号,每个引号都用2个代码编码,因此我希望合并它们,并有2行而不是4行,这样代码变量中的1和0相加,但年份、段和参与者变量保持不变,因为它们是相同的 应该是这样的:

desiredoutput<-tibble(segments=c('Brexit is bad','We need a sit on the table'),
   actor=c("SNP", "Labour"),
   year=c(2015, 2017),
   TL_Brexit=c(1,0),
   Bre_negative=c(1,0),
   TL_participation=c(0,1),
   TD_other=c(0,1))
欢迎任何帮助

如果按细分、参与者和年份分组,则可以通过取其他列的总和来总结每组

library(dplyr)

example %>% 
  group_by(segments, actor, year) %>% 
  summarise_all(sum)

# # A tibble: 2 x 7
# # Groups:   segments, actor [2]
#   segments                 actor  year TL_Brexit Bre_negative TL_participation TD_other
#   <chr>                    <chr> <dbl>     <dbl>        <dbl>            <dbl>    <dbl>
# 1 Brexit is bad            SNP    2015         1            1                0        0
# 2 We need a sit on the ta~ Labo~  2017         0            0                1        1