Hi all—I am trying to run a for loop that creates a new variable based on some conditions over a vector of multiple dataframes in R (that are split by observation ID), and am having some trouble.
Dat_nations <- split(Dat, Dat$newccode)
^This creates my vector of 143 dataframes, grouped by country code. The for loop that I want to apply to each country dataframe is:
for (i in 1:(length(df1$timeafter)
-1){
df1$timeafter[i+1] <- (df1$newdate[i+1]-df1$newdate[i])
}
Essentially, I am creating a new variable that counts the number of days an observation came after the preceding observation from within a specific country (they are arranged in order by date). But I can't figure out how to run this over all dataframes iteratively, modifying each, and then combining them all back together.
Thanks so much!