Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a delta column to plot time series differences in R

Tags:

r

I have a set of motorsport laptime data (mld) of the form:

  car lap laptime
1  1   1 138.523
2  1   2 122.373
3  1   3 121.395
4  1   4 137.871

and I want to produce something of the form:

  lap  car.1    car.1.delta   
1  1   138       NA
2  2   122       -16  
3  3   121       -1  
4  4   127       6

I can use the R command diff(mld$laptime, lag=1) to produce the difference column, but how do I elegantly create the padded difference column in R?

like image 651
psychemedia Avatar asked Sep 26 '11 08:09

psychemedia


1 Answers

Here are a couple of approaches:

1) zoo

If we represented this as a time series using zoo then the calculation would be particularly simple:

# test data with two cars

Lines <- "car lap laptime
1   1 138.523
1   2 122.373
1   3 121.395
1   4 137.871
2   1 138.523
2   2 122.373
2   3 121.395
2   4 137.871"
cat(Lines, "\n", file = "data.txt")

# read it into a zoo series, splitting it
# on car to give wide form (rather than long form)

library(zoo)
z <- read.zoo("data.txt", header = TRUE, split = 1, index = 2, FUN = as.numeric)

# now that its in the right form its simple

zz <- cbind(z, diff(z))

The last statement gives:

> zz
      1.z     2.z 1.diff(z) 2.diff(z)
1 138.523 138.523        NA        NA
2 122.373 122.373   -16.150   -16.150
3 121.395 121.395    -0.978    -0.978
4 137.871 137.871    16.476    16.476

To plot zz, one column per panel, try this:

plot(zz, type = "o")

To only plot the differences we do not really need zz in the first place as this will do:

plot(diff(z), type = "o")

(Add the screen=1 argument to the plot command to plot everything on the same panel.)

2) ave. Here is a second solution that uses just plain R (except for the plotting) and keeps the output in long form; however, it is a bit more complex:

# assume same input as above

DF <- read.table("data.txt", header = TRUE)
DF$diff <- ave(DF$laptime, DF$car, FUN = function(x) c(NA, diff(x)))

The result is:

> DF
  car lap laptime    diff
1   1   1 138.523      NA
2   1   2 122.373 -16.150
3   1   3 121.395  -0.978
4   1   4 137.871  16.476
5   2   1 138.523      NA
6   2   2 122.373 -16.150
7   2   3 121.395  -0.978
8   2   4 137.871  16.476

To plot just the differences, one per panel, try this:

library(lattice)
xyplot(diff ~ lap | car, DF, type = "o")

Update

Added info above on plotting since the title of the question mentions this.

like image 86
G. Grothendieck Avatar answered Sep 22 '22 15:09

G. Grothendieck