Mapping with nested time series

I've run into an interesting problem, which I can not quite get around (as the solution seems to cause problems when I want to map and mutate nested datasets. When I use parse_date_time on a time series panel set like so...

df<-structure(list(date_strt = structure(c(18450, 18434, 18418, 18402, 
18466, 18498, 18514, 18530, 18482, 18786, 18802, 18306, 18290, 
18370, 18386), class = "Date"), item_id = c(47010, 47010, 47010, 
47010, 47010, 47010, 47010, 47010, 47010, 47010, 47010, 47010, 
47010, 47010, 47010), locn_id = c(74, 74, 74, 74, 74, 74, 74, 
74, 74, 74, 74, 74, 74, 74, 74), sale_unit = c(63, 57, 43, 58, 
65, 58, 189, 91, 52, 9, 10, 47, 22, 47, 67), rprc_unit_amnt = c(1.45, 
1.45, 1.45, 1.45, 1.45, 1.45, 1.45, 1.45, 1.45, 1.45, 1.45, 1.45, 
1.45, 1.45, 1.45), sale_unit_amnt = c(1.45, 1.45, 1.45, 1.45, 
1.45, 1.45, 0.962380952380952, 1.29208791208791, 1.45, 1.30444444444444, 
1.301, 1.45, 1.45, 1.45, 1.45), prmt_ind = c(0, 0, 0, 0, 0, 0, 
1, 1, 0, 1, 1, 0, 0, 0, 0), cluster = c(7, 7, 7, 7, 7, 7, 7, 
7, 7, 7, 7, 7, 7, 7, 7)), row.names = c(NA, -15L), class = c("tbl_df", 
"tbl", "data.frame"))

This works perfectly fine (although it also seems to cause issues for later mapping of mutates), but, is slow(ish) for when scaling up to 4mill+ rows.

df %>%
  unnest(cols = c(data)) %>%
  mutate(date_strt = lubridate::parse_date_time(date_strt, c("ymd", "dmy")))

When this data is nested with an item grouping (df %>% mutate(item_grup_id = 6231) (I'm assuming you can repeat the same df with another grouping number and use this instead of a full repex of the nested df). Anyhow I get this error if I attempt to do this in a nested mutate

nested_df %>%  
  mutate(data = map(data, function(df){
    df %>% 
      mutate(date_strt = lubridate::parse_date_time(date_strt, c("ymd", "dmy")))
  }))
Error: Problem with `mutate()` column `data`.
i `data = map(...)`.
x no applicable method for 'mutate' applied to an object of class "Date"

If I do the full (slow) dataset parsing, and map a mutate later with a similar coding structure, I get pretty much the same error

nested_df %>%  
    mutate(data = map(data, function(df){
      df %>%
        mutate(unite(ITEMDECPRO, item_id, prmt_ind, remove = FALSE))
    }))
Error: Problem with `mutate()` column `data`.
i `data = map(...)`.
x no applicable method for 'mutate' applied to an object of class "c('POSIXct', 'POSIXt')"
i The error occurred in row 1.

Does anyone have any ideas about how to do this without causing an error, and also why this occurs?
Thanks for reading

Seems that orginal nest_by() caused issues that are not present when using group_by() then then nest() followed by

nested_df %>%
  mutate(data = future_map(
    data, 
    ~ .x %>%
      mutate(date_strt = lubridate::parse_date_time(date_strt, c("ymd", "dmy")))
  ))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.