0

I have a dataframe and I want to create several dataframes based on a column ('age'). Now I know only how to do it manually, filtering considering the age column. But I should not do this every time that appears a new value in the column.

My original dataframe has 20 different values in that column and I don't want to repeat the code every time.

Reduced dataframe:

df <- data.frame(name = c("A", "B", "A", "C", "B", "B", "B", "C", "A"),
age = c(11, 11, 11, 12, 12, 11, 13, 12, 11))

My code:

df_11 <- df %>%
  filter(age == 11)

df_12 <- df %>%
  filter(age == 12)

df_13 <- df %>%
  filter(age == 13)

My result should be:

df_11 <- data.frame(name = c("A", "B", "A", "B", "A"), 
age = c(11, 11, 11, 11, 11))

df_12 <- data.frame(name = c("C", "B", "C"),
age = c(12, 12, 12))

df_13 <- data.frame(name = c("B"),
age = c(13))
polo
  • 185
  • 1
  • 3
  • 11
  • 1
    Are you looking for `split(df, df$age)`? Of course this will put them in a list and not on your global environment, which is better anyway – Sotos Jun 23 '20 at 12:53

1 Answers1

1

One approach is with split and list2env:

dflist <- split(df,df$age)
names(dflist) <- paste0("df_",names(dflist))
list2env(dflist,envir = globalenv())
ls(pattern = "df")
[1] "df"    "df_11" "df_12" "df_13" 

Although I agree with @Sotos that a list is probably better.

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57