Search code examples
rmatrixtimediffdifference

Calculate all time differences between points in R (instead of from one point to the next one)


I would like to calculate the differences in time from POSIXct format. I am able to calculate the differences between consecutive points using

diff(data$time)

but not from all against all. So I guess my data is at least correctly imported.

I actually want to calculate all distances between points of one individual, so my data looks like: Posix, individual, otherinfo. If there is a simple way i would love to calculate automaticly the differences from all points per indiviual. If its not so straight forward I will do data subsets per individual thats fine.

I would be happy if someone could help me! I tried

     dist(data$time)

because I know its a distance matrix calculation tool but unfortunalety it just gives me a list of rising numers (1,2,3,...) so i guess it is not familiar with the time format..

Thanks a lot!


Solution

  • We can use sapply

    sapply(data$time, `-`, data$time)
    

    or with outer

    outer(data$time, data$time, FUN = `-`)