Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Group dataframe by ID and then split it into multiple dataframes for each group

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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() 

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading