I have a data frame that is made up of mostly sequential rows. Mostly meaning that some are out of sequence or missing. When the the sequential row for the current row is present, I'd like to perform some function using data from both rows. If it's not present, skip it and move on. I know I can do this with a loop, but it's quite slow. I think this has something to do with using the index. Here is an example of my problem using sample data and a desired result that uses a loop.
df <- data.frame(id=1:10, x=rnorm(10))
df <- df[c(1:3, 5:10), ]
df$z <- NA
dfLoop <- function(d)
{
for(i in 1:(nrow(d)-1))
{
if(d[i+1, ]$id - d[i, ]$id == 1)
{
d[i, ]$z = d[i+1, ]$x - d[i, ]$x
}
}
return(d)
}
dfLoop(df)
So how might I be able to get the same result without using a loop? Thanks for any help.