I have a dataframe with 6 columns. Column 1 holds dates, column 2 individuals and column 3 to 6 the are used for a calcuation.
Date <- c(1, 1, 2, 2, 2, 3)
Ind <- c("a","a","a","b","c","c")
C <- c(5, 6, 5, 7, 8, 8)
D <- c(8, 8, 9, 9, 9, 9)
E <- c(8, 9, 11, 10, 9, 7)
F <- c(5, 6, 8, 5, 7, 4)
df <- data.frame(Date, Ind, C, D, E, F)
I want to perform a calculation (like (C-E)+(D-F) (in real life those are coordinates and I'm calculating distances, but that's not where my problem is right now).
I want to perform the calculations, stored in a new column (G), with a 1 day difference between where I use the value of column C and E from day 1, and the values of column E and F from day+1 for the same individual.
I'm not sure if I should use a loop or an apply function. This is what I've tried so far, a vectorized operation and subsetting, based on this thread: Loop over rows of dataframe applying function with if-statement
df$G <- NA
df[!(df$Date ==(df$Date+1)), "G"] <- ((C-E)+(D-F))
This works, but it does the calculations on the coordinates from the same row (C, D, E, F all from same row). I realize why it does this, as I don't state from which row to take the coordinates. C and D need to be taken from row where Date = Date, and E and F from row where Date = (Date+1). I realize it, but I can't get my head around it how to do that.
Continue this route? Do it in a for loop? Using apply function?