Reputation: 175
I have the following data frame:
df <- read.csv("data.csv", header=TRUE)
> df
ID Time Expectation
1 cat 1.1 1
2 dog 1.0 2
3 dog 1.1 3
4 guinea_pig 1.0 7
5 cat 1.0 1
6 guinea_pig 3.8 10
7 cat 0.8 1
8 cat 2.1 1
9 cat 3.6 1
10 guinea_pig 3.0 16
11 dog 0.9 2
12 guinea_pig 2.7 15
13 guinea_pig 4.0 10
14 dog 5.2 6
15 dog 7.2 7
Somebody has helped me make a list of expectation values for each variable in the ID column in increasing order of time, as follows:
library("dplyr")
df <- df %>%
group_by(ID) %>%
summarise(var = list(Expectation[order(Time)]))
> df
# A tibble: 3 x 2
ID var
<fct> <list>
1 cat <int [5]>
2 dog <int [5]>
3 guinea_pig <int [5]>
How can I convert the above into a list of time series objects, while retaining the "cat"/"dog"/"guinea_pig" label so I can identify which series is which?
Many thanks!
Upvotes: 0
Views: 481
Reputation: 388982
We can use map
/lapply
and apply ts
function to convert into time-series object.
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"))
Upvotes: 2