library(tidyverse)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
df <- tribble(~id, ~date,
2380, "10/30/12",
2380, "10/31/12",
2380, "11/1/12",
2380, "11/2/12",
20100, "10/30/12",
20100, "10/31/12",
20100, "11/1/12",
20100, "11/2/12",
20103, "10/30/12",
20103, "10/31/12")
df %>%
mutate(date = mdy(date)) %>%
group_by(id) %>%
mutate(date_difference = as.numeric(date - first(date)))
#> # A tibble: 10 x 3
#> # Groups: id [3]
#> id date date_difference
#> <dbl> <date> <dbl>
#> 1 2380 2012-10-30 0
#> 2 2380 2012-10-31 1
#> 3 2380 2012-11-01 2
#> 4 2380 2012-11-02 3
#> 5 20100 2012-10-30 0
#> 6 20100 2012-10-31 1
#> 7 20100 2012-11-01 2
#> 8 20100 2012-11-02 3
#> 9 20103 2012-10-30 0
#> 10 20103 2012-10-31 1
Создано в 2018-11-29 пакетом представ (v0.2.1)