I have a "big" data frame where I need to do a calcul like below :
data <- data.frame( "name"=c( "Tom", "Peter", "Peter", "Peter", "Tom", "Peter" ), "goal"=c(1,-2,2,3,-1,0), "total"=0 )
for( i in 1:nrow(data) ) {
count <- 0
for ( j in 1:i) {
if (data$name[j] == data$name[i]) {
count <- count + data$goal[j]
}
}
data$total[i] <- count
}
> data
name goal total
1 Tom 1 1
2 Peter -2 -2
3 John 2 2
4 Peter 3 1
5 Tom -1 0
6 Peter 0 1
I need to perform the calculation of the "total" column by adding the goals scored before.
My database is currently 83000 rows long and the calculation is very long. I would like to do this calculation without a "for" loop. Do you have an idea ?
I saw the following post but I don't know how to adapt it.
Thanks in advance