1

I would like to calculate a new variable "acceleration (in m/s^2)" for longitudinal recorded car data and only have the speed of the car(in m/s) and the recording time (in s).

This is a part of the dataframe df:

 speed time_sec
1 27.520    0.273
2 27.520    0.313
3 27.172    0.353
4 26.887    0.393
5 26.560    0.433
6 26.560    0.473

How could I add a new variable "acceleration (positive values)/ deceleration (negative values)" to every recording row of the car?

1 Answers1

0

This is basically delta speed divided by delta time. So use diff twice and don't forget the first value is not computable from the data.

c(NA, with(cars, diff(speed)/diff(time_sec)))
#[1]     NA  0.000 -8.700 -7.125 -8.175  0.000

cars$accel <- c(NA, with(cars, diff(speed)/diff(time_sec)))

Data.

cars <- read.table(text = "
 speed time_sec
1 27.520    0.273
2 27.520    0.313
3 27.172    0.353
4 26.887    0.393
5 26.560    0.433
6 26.560    0.473                   
", header = TRUE)
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66