3

I have a dataset like this

V0  V1  V2 
1   A   21 
2   A   21  
3   B   22  
4   C   23  
5   C   23  

So there are duplicated value in V1; and I would like to create a dataset like this, which sum up V2 but keeping the duplicates in V1:

V0  V1  V2 
1   A   42 
2   A   42  
3   B   22  
4   C   46  
5   C   46

thanks

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66

2 Answers2

3

We can use ave from base R

df1$V2 <- with(df1, ave(V2, V1, FUN = sum))

Or with dplyr

library(dplyr)
df1 %>%
   group_by(V1) %>%
   mutate(V2 = sum(V2))
akrun
  • 874,273
  • 37
  • 540
  • 662
2

We can also use data.table

library(data.table)
setDT(dt)[,V2:=sum(V2),by=V1]
Rushabh Patel
  • 2,672
  • 13
  • 34