1

I have a dataframe that looks like:

library(tidyverse)
df <- tibble::tribble(
  ~id,    ~list,
    1,  "apple",
    1,   "pear",
    1, "banana",
    2,  "apple",
    2, "banana",
    3,  "apple"
  )

I want create a list of the unique elements of the column df$list

unique(df$list)
unique_list <- c("apple", "pear", "banana")

I want to iterate over the main df over the each value of unique_list and filter the whole df and create 3 different dataframe as a results, named df_"each element of the list".

I can do it for individual elements like this:

df %>% filter(list = 'apple') -> df_apple

# and:
df %>% filter(list = 'pear') -> df_pear

# and so on...

So eventually I´ll have 3 filtered dataframe, df_apple, df_pear, df_apple.

How can I make this process more concise?

Oka
  • 1,318
  • 6
  • 11
chopin_is_the_best
  • 1,951
  • 2
  • 23
  • 39

0 Answers0