Add value from previous row under conditions
问题 I have a df data and I would like to add to a new column a value that exist in a previous column and row if the factor is the same. Here is a sample: data <- structure(list(Id = c("a", "b", "b", "b", "a", "a", "b", "b", "a", "a"), duration.minutes = c(NA, 139L, 535L, 150L, NA, NA, 145L, 545L, 144L, NA), event = structure(c(1L, 4L, 3L, 4L, 2L, 1L, 4L, 3L, 4L, 2L), .Label = c("enter", "exit", "stop", "trip" ), class = "factor")), .Names = c("Id", "duration.minutes", "event" ), class = "data