Мы можем использовать map
/ lapply
и применять функцию ts
для преобразования в объект временных рядов.
library(dplyr)
df %>%
group_by(ID) %>%
summarise(var = list(Expectation[order(Time)]),
var_ts = purrr::map(var, ts))
#Or with lapply
#var_ts = lapply(var, ts))
# A tibble: 3 x 3
# ID var var_ts
# <fct> <list> <list>
#1 cat <int [5]> <ts>
#2 dog <int [5]> <ts>
#3 guinea_pig <int [5]> <ts>
data
df <- structure(list(ID = structure(c(1L, 2L, 2L, 3L, 1L, 3L, 1L, 1L,
1L, 3L, 2L, 3L, 3L, 2L, 2L), .Label = c("cat", "dog", "guinea_pig"
), class = "factor"), Time = c(1.1, 1, 1.1, 1, 1, 3.8, 0.8, 2.1,
3.6, 3, 0.9, 2.7, 4, 5.2, 7.2), Expectation = c(1L, 2L, 3L, 7L,
1L, 10L, 1L, 1L, 1L, 16L, 2L, 15L, 10L, 6L, 7L)), class = "data.frame",
row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15"))