Home > OS >  How to split a dataframe into a list keeping the previous dataframe in R?
How to split a dataframe into a list keeping the previous dataframe in R?

Time:10-19

I have this data frame

This is a minimal reproducible example of my data frame

value <- c(rnorm(39, 5, 2))
Date <- seq(as.POSIXct('2021-01-18'), as.POSIXct('2021-10-15'), by = "7 days")

df <- data.frame(Date, value)

# This is the vector I have to compare with the Date of the dataframe
dates_tour <- as.POSIXct(c('2021-01-18', '2021-05-18', '2021-08-18', '2021-10-15'))

df <- df %>%
  mutate(
    tour = cut(Date, breaks = dates_tour, labels = seq_along(dates_tour[-1]))
  )

Now that I have the data frame label on each group based on the dates_tour I want to split the data frame based on the tour factor but I need that each list contains the data frame of the previous data frame. For instance df_list[[1]] contains the rows with tour == 1The second list needs to contain the first and the second data frame tour == 1 | tour == 2. The third list needs to contain the first, second, and third data frames and so on. I need to work writing a general code that works with different lengths of dates_tour as sometimes it can contain different lengths of values.

This code creates a list based on the tour value

df_list = split(df, df$tour)

But is not useful to create what I need

CodePudding user response:

We may use a loop for that

df_list <- lapply(unique(df$tour), function(x) subset(df, tour %in% seq_len(x)))

CodePudding user response:

You could also do:

Reduce(rbind, split(df, ~tour), accumulate = TRUE)

if you have an older version of R:

Reduce(rbind, split(df, df$tour), accumulate = TRUE)

You could also use accumulate from purrr:

library(purrr)
accumulate(split(df, ~tour), rbind)
  • Related