I'm an unexperienced user of R and I need to create quite a complicated stuff. My dataset looks like this :
a,b,c,d,e are different individuals. I want to complete the D column as follows : At the last line for each individual in the col A, D = sum(C)/(B-1).
Expected results should look like :
D4=sum(C2:C4)/(B4-1)=0.5
D6=sum(C5:C6)/(B6-1)=1, etc.
I attempted to deal with it with something like :
for(i in 2:NROW(dataset)){
dataset[i,4]<-ifelse(
(dataset[i,1]==data1[i-1,1]),sum(dataset[i,3])/(dataset[i,2]-1),NA
)
}
But it is obviously not sufficient, as it computes the D value for all the rows and not only the last for each individual, and it does not calculate the sum of C values for this individual.
And I really don't know how to figure it out. Do you guys have any advice ? Many thanks.