Hello all!
I am trying to iteratively add new observations to my dataframe df1. Instead of using rbind() to insert the observations at the end of the dataframe, I'd like to insert my observations into df1 using add_row(). I'm relatively new to R, so currently I iterate by running a for loop with the range as
row in 1:nrow(df1))
and then (given some conditions), creating a new row and using rbind to append it to df1. If I wanted to instead insert the row inside df1 (i.e. if it happens at row n, inserting at n + 1), could I still use the same range for my loop? If I have k original rows, and I insert an observation at, say, row 5, now I have k + 1 rows, and the former row 5 would instead be row 6, so I would think that I'd need to adjust my loop to a larger range. Is this true?
Also worth noting, I've been reading that it's better to vectorize operations in R because it works faster than a for loop. Is there a way to vectorize this operation? Or any recommendations on tutorials/websites where I can read up and start better understanding vectorizing?
Thanks so much.