-2

I have let's say a file formatted like this

id  votes   irrelevantcolumn
1   10  abcd

5   28  abcd

1   30  abcd

9   45  abcd

3   23  abcd

3   21  abcd

2   46  abcd

7   90  abcd

9   12  abcd

What I want is to get the sum for every of the ids e.g. i want to get a new table that will be like

id  votes

1   40

2   46

3   44

5   28

7   90

9   57
David Arenburg
  • 89,637
  • 17
  • 130
  • 188
Basilis
  • 29
  • 6

1 Answers1

1

Try aggregate from base R

aggregate(votes~id, df, FUN=sum)
#   id votes
#1  1    40
#2  2    46
#3  3    44
#4  5    28
#5  7    90
#6  9    57

or

library(dplyr)
df %>%
    group_by(id) %>%
    summarise(votes=sum(votes))        

or

library(data.table)
setDT(df)[, list(votes=sum(votes)), by=id] 
akrun
  • 789,025
  • 32
  • 460
  • 575