3

Let's say I have a timeseries dataset with different IDs. Each ID has different values. I would like to first group the data by IDs. Then I would split this dataframe into Multiple dataframes for each group.

How can I do this in R using dplyr?

# Sample Data

ID = c("A", "B", "C", "A", "B", "C", "A", "B", "C")
Date = c("01/01/2022", "01/02/2022", "01/03/2022", "01/01/2022", "01/02/2022", "01/03/2022", "01/01/2022", "01/02/2022", "01/03/2022")
Value = c("45", "24", "33", "65", "24", "87", "51", "32", "72")

What I want:

df1

       ID       Date   Value
    1  A 01/01/2022    45
    2  A 01/02/2022    65
    3  A 01/03/2022    51
df2

       ID       Date   Value
    1  B 01/01/2022    24
    2  B 01/02/2022    24
    3  B 01/03/2022    32
df3

       ID       Date   Value
    1  C 01/01/2022    33
    2  C 01/02/2022    87
    3  C 01/03/2022    72



library(dplyr)

df = (ID, Date, Value)

    # What I tried so far
    
    df_group =  df  %>% 
      group_by(ID, Value) %>% 
      group_split() 
Ed_Gravy
  • 1,841
  • 2
  • 11
  • 34

2 Answers2

4

Per answer, you want to set up your df as

df <- data.frame(ID, Date, Value)

You can then run the group_split

dataframe <- df %>%
group_split(ID)
Brian Syzdek
  • 873
  • 6
  • 10
0

Creating the data frame:

ID = c("A", "B", "C", "A", "B", "C", "A", "B", "C")
    Date = c("01/01/2022", "01/02/2022", "01/03/2022", "01/01/2022", "01/02/2022", "01/03/2022", "01/01/2022", "01/02/2022", "01/03/2022")
    Value = c("45", "24", "33", "65", "24", "87", "51", "32", "72")

df <- data.frame(ID,Date,Value)

Splitting the data:

df_a <- df %>% 
  filter(ID =="A")
df_b <-  df %>% 
  filter(ID =="B")
df_c <-  df %>% 
  filter(ID =="C")

Printing the data:

Now just run the split data frames below:

df_a
df_b
df_c

This will give you the following output:

  ID       Date Value
1  A 01/01/2022    45
2  A 01/01/2022    65
3  A 01/01/2022    51

  ID       Date Value
1  B 01/02/2022    24
2  B 01/02/2022    24
3  B 01/02/2022    32

  ID       Date Value
1  C 01/03/2022    33
2  C 01/03/2022    87
3  C 01/03/2022    72
Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30