Hi I want to create an experience variable from a dataframe looking similar to this:
ID Year Experience gained that year
1 2000 1
1 2001 3
1 2002 1
I am trying to do something similar to this:
NewDF <- DF %>% mutate(Cummulated_Exp = sum(DF[which(DF$ID == ID & DF$year < year),3]))`
resulting in
ID Year Experience gained that year Cummulated_EXp
1 2000 1 0
1 2001 3 3
1 2002 1 4
This wont work for a variety of reasons, likely something to do with the data i give to the sum()
function as the ultimate error i always arrive at
Error in FUN(X[[i]], ...) :
only defined on a data frame with all numeric variables
Thanks for the help