Home > other >  dataframe in wideformat to dataframe of timeseries
dataframe in wideformat to dataframe of timeseries

Time:09-30

I am currently struggling with reshaping my dataset to a preferred result. Lets say I have the following dataset to start with:

library(tsbox)
library(dplyr)    
library(tidyr)

# create df that matches my format
df1 <- ts_wide(ts_df(ts_c(mdeaths)))
df1$id <- 1
df2 <- ts_wide(ts_df(ts_c(mdeaths)))
df2$id <- 2
df <- rbind(df1, df2)

Now this dataset has a date column, a value column and an "id" column, which should specifiy which date/value points belong to the same observation object. I would now like to reshape my dataset to a 2x2 dataframe, where the first column is the id, while the second column is a timeseries object (of the date/value corresponding to that id). To do so, I tried the following:

# create a new df, with two cols (id and ts)
df_ts <- df %>% 
group_by(id) %>%
nest()

The nest command creates a "a list-column of data frames", which is not exactly what I wanted. I know that a ts can be defined via ts(data$value, data$date), but I do not know how to integrate it after the group_by(id) function. Can anyone help me how to turn this column into a ts object instead of a data frame? I am new to R and grateful for any form of help. Thanks in advance

CodePudding user response:

If you have a non-atomic data type it will have to be a list column of something.
If you want a list-column of ts object you can:

df %>%
  group_by(id) %>%
  summarize(ts = list(ts(value, time)))

Continuing your pipe you could:

df %>% 
  group_by(id) %>% 
  nest() %>% 
  mutate(data = purrr::map(data, with, ts(value, time)))
  • Related