mardi 21 septembre 2021

R: How to fill out values in a DF which are dependent on previous rows

I have a dataframe, and I want to do some calculations depending on the previous rows (like dragging informations down in excel). My DF looks like this:

set.seed(1234)
df <- data.frame(DA = sample(1:3, 6, rep = TRUE) ,HB = sample(0:600, 6, rep = TRUE), D = sample(1:5, 6, rep = TRUE), AD = sample(1:14, 6, rep = TRUE), GM = sample(30:31, 6, rep = TRUE), GL = NA, R =NA, RM =0  )
df$GL[1] = 646
df$R[1] = 60
df$DA[5] = 2

df
#   DA  HB D AD GM  GL  R RM
# 1  2 399 4 13 30 646 60  0
# 2  2  97 4 10 31  NA NA  0
# 3  1 102 5  5 31  NA NA  0
# 4  3 325 4  2 31  NA NA  0
# 5  2  78 3 14 30  NA NA  0
# 6  1 269 4  8 30  NA NA  0

I want to fill out the missing values in my GL, R and RM columns, and the values are dependent on each other. So eg.

attach(df)

#calc GL and R for the 2nd row

df$GL[2] <- GL[1]+HB[2]+RM[1]

df$R[2] <- df$GL[2]*D[2]/GM[2]*AD[2]

#calc GL and R for the 3rd row

df$GL[3] <- df$GL[2]+HB[3]+df$RM[2]
df$R[3] <-df$GL[3]*D[3]/GM[3]*AD[3]

#and so on..

Is there a way to do all the calculations at once, instead of row by row?

In addition, each time the column 'DA' = 2, the previous values for 'R' should be summed up for the same row for 'RM', but only from the last occurence. So that

attach(df)

df$RM[3] <-R[1]+R[2]+R[3]

#and RM for the 6th row is calculated by

#df$RM[6] <-R[4]+R[5]+R[6]

Thanks a lot in advance!

Aucun commentaire:

Enregistrer un commentaire