# [R] Calculate mileage at each 'faildate'

Abhinaba Roy abhinabaroy09 at gmail.com
Wed Aug 10 11:22:53 CEST 2016

Hi R-helpers,

- I have a dataframe similar to 'simD'.

> dput(simD)
structure(list(ID = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("A", "B"), class = "factor"), PRODDATE =
structure(c(14655,
14655, 14655, 14655, 14664, 14664, 14664, 14664, 14664, 14664
), class = "Date"), FAILDATE = structure(c(15053, 15054, 15057,
15058, 14844, 14875, 14876, 14905, 14936, 14966), class = "Date"),
MILEAGE = c(21548L, 22578L, 22868L, 23654L, 30245L, 32148L,
34128L, 35879L, 39874L, 40125L)), .Names = c("ID", "PRODDATE",
"FAILDATE", "MILEAGE"), row.names = c(NA, -10L), class = "data.frame")

- I have split the dataset by 'ID' and sorted the dataframe by

- Now, for each of the splits I want to calculate the 'Mileage' at each
of the 'faildate'.

- The output I desire is 'outD'.

> dput(outD)
structure(list(ID = structure(1:2, .Label = c("A", "B"), class = "factor"),
PRODDATE = structure(c(14655, 14664), class = "Date"), MIL_1 =
c(21548L,
30245L), MIL_2 = c(1030L, 1903L), MIL_3 = c(290L, 1980L),
MIL_4 = c(786L, 1751L), MIL_5 = c(NA, 3995L), MIL_6 = c(NA,
251L)), .Names = c("ID", "PRODDATE", "MIL_1", "MIL_2", "MIL_3",
"MIL_4", "MIL_5", "MIL_6"), row.names = c(NA, -2L), class = "data.frame")

***Please note that I have MIL_6 because the max(# failures) in data by ID
is 6 (6 failures for 'B')*

- And I would like to extend it to other numeric and date variable as
well.

How can I do this in R?

Best,
Abhinaba

[[alternative HTML version deleted]]