Closed VirginiaMorera closed 1 year ago
I usually add a column in the map
call. Something like this:
test_tracks %>%
mutate(data = map(data, ~ .x %>% mutate(sl = step_lengths(.)))) |>
unnest(cols = data)
# Or with native pipes
test_tracks |>
mutate(data = map(data, ~ .x |> (\(x) mutate(x, sl = step_lengths(x)))())) |>
unnest(cols = data)
A function like add_sl()
would be nice, but I did not find time to implement it.
I am not sure this is so much an amt issue or a tidyr issue, but I've encountered it only with tracks, so let's start here. When working with nested tracks, any calculation (e.g. step_lengths) done in the nested tracks creates a new "list-like" column outside the "data" column, with the same number of rows. I am not sure that is the desired outcome, I'd rather have it added to the nested dataset inside the "data" column, but that's fine, it can be easily solved by unnesting and nesting again (in theory). However, unnest produces this weird list-like object of class NULL, and although I have managed to find a workaround to turn it back into a dataframe, then I need to use make_tracks and nest again to go back to the starting point, which is annoying. Code below with a reprex
First make the dataset
Now make it a nested track
Calculate step lengths
This produces a nested df that has an additional "list" column, sl
Unnest
This produces a list, but of class NULL
I can turn this list back to a normal df with do.call, but other dplyr or tibble methods won't work
However it is a plain df now, and all the variables have lost their format (they are all
chr
) which is not ideal.Any idea why this is happening and how to fix it?