purrr rbind list of data frames per groupCombine every ith element of a list of lists together using dplyr, purrrDrop factor levels in a subsetted data frameHow to join (merge) data frames (inner, outer, left, right)Convert a list of data frames into one data frameR - list to data frameDrop data frame columns by nameChanging column names of a data frameChange and save only one row in a filepurrr list evaluation strangenessError for neuralnet package in RUsing flextable in r markdown loop not producing tables

Markov-chain sentence generator in Python

Understanding this peak detector circuit

Why are Tucker and Malcolm not dead?

TEMPO: play a sound in animated GIF/PDF/SVG

0xF1 opcode-prefix on i80286

How to disable "Completion time:..." in SQL Server Messages window

Heating Margarine in Pan = loss of calories?

What ability do tools use?

How to assign many blockers at the same time?

How to describe accents?

If a digital camera can be "hacked" in the ransomware sense, how best to protect it?

A torrent of foreign terms

Safest way to store environment variable value in a file

Why are Gatwick's runways too close together?

How can Radagast come across Gandalf and Thorin's company?

What gave Harry Potter the idea of writing in Tom Riddle's diary?

Submitting a new paper just after another was accepted by the same journal

Email address etiquette - Which address should I use to contact professors?

What is my malfunctioning AI harvesting from humans?

Why is the result of ('b'+'a'+ + 'a' + 'a').toLowerCase() 'banana'?

What should I call bands of armed men in the Middle Ages?

visible indication that a cell is not evaluatable

These were just lying around

Is there a Morita cocycle for the mapping class group Mod(g,n) when n > 1?



purrr rbind list of data frames per group


Combine every ith element of a list of lists together using dplyr, purrrDrop factor levels in a subsetted data frameHow to join (merge) data frames (inner, outer, left, right)Convert a list of data frames into one data frameR - list to data frameDrop data frame columns by nameChanging column names of a data frameChange and save only one row in a filepurrr list evaluation strangenessError for neuralnet package in RUsing flextable in r markdown loop not producing tables






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















After using purrr and friends to read in a load of csvs I have ended up with a tibble that looks something like this:



library(tidyverse)

df <-
tibble(
df_name = c("A", "B", "A", "A", "B"),
data = list(iris)
)

df

# A tibble: 5 x 2
df_name data
<chr> <list>
1 A <data.frame [150 × 5]>
2 B <data.frame [150 × 5]>
3 A <data.frame [150 × 5]>
4 A <data.frame [150 × 5]>
5 B <data.frame [150 × 5]>


I want to rbind (or equivalent) all data with a common df_name. I'd like the output to be a named list. I can do this with tapply:



desired = tapply(df$data, df$df_name, function(y) do.call(rbind,y)) 

List of 2
$ A:'data.frame': 450 obs. of 5 variables:
..$ Sepal.Length: num [1:450] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
..$ Sepal.Width : num [1:450] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
..$ Petal.Length: num [1:450] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
..$ Petal.Width : num [1:450] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
$ B:'data.frame': 300 obs. of 5 variables:
..$ Sepal.Length: num [1:300] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
..$ Sepal.Width : num [1:300] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
..$ Petal.Length: num [1:300] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
..$ Petal.Width : num [1:300] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
- attr(*, "dim")= int 2
- attr(*, "dimnames")=List of 1
..$ : chr [1:2] "A" "B"


I can't figure out how to do the same with purrr verbs. I think perhaps I need to start by setting the list names:



df_p <- 
df %>%
mutate(data = setNames(data, df_name))


I found this question but I can't figure out how to apply in this situation.










share|improve this question






























    2















    After using purrr and friends to read in a load of csvs I have ended up with a tibble that looks something like this:



    library(tidyverse)

    df <-
    tibble(
    df_name = c("A", "B", "A", "A", "B"),
    data = list(iris)
    )

    df

    # A tibble: 5 x 2
    df_name data
    <chr> <list>
    1 A <data.frame [150 × 5]>
    2 B <data.frame [150 × 5]>
    3 A <data.frame [150 × 5]>
    4 A <data.frame [150 × 5]>
    5 B <data.frame [150 × 5]>


    I want to rbind (or equivalent) all data with a common df_name. I'd like the output to be a named list. I can do this with tapply:



    desired = tapply(df$data, df$df_name, function(y) do.call(rbind,y)) 

    List of 2
    $ A:'data.frame': 450 obs. of 5 variables:
    ..$ Sepal.Length: num [1:450] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
    ..$ Sepal.Width : num [1:450] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
    ..$ Petal.Length: num [1:450] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
    ..$ Petal.Width : num [1:450] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
    ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
    $ B:'data.frame': 300 obs. of 5 variables:
    ..$ Sepal.Length: num [1:300] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
    ..$ Sepal.Width : num [1:300] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
    ..$ Petal.Length: num [1:300] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
    ..$ Petal.Width : num [1:300] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
    ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
    - attr(*, "dim")= int 2
    - attr(*, "dimnames")=List of 1
    ..$ : chr [1:2] "A" "B"


    I can't figure out how to do the same with purrr verbs. I think perhaps I need to start by setting the list names:



    df_p <- 
    df %>%
    mutate(data = setNames(data, df_name))


    I found this question but I can't figure out how to apply in this situation.










    share|improve this question


























      2












      2








      2








      After using purrr and friends to read in a load of csvs I have ended up with a tibble that looks something like this:



      library(tidyverse)

      df <-
      tibble(
      df_name = c("A", "B", "A", "A", "B"),
      data = list(iris)
      )

      df

      # A tibble: 5 x 2
      df_name data
      <chr> <list>
      1 A <data.frame [150 × 5]>
      2 B <data.frame [150 × 5]>
      3 A <data.frame [150 × 5]>
      4 A <data.frame [150 × 5]>
      5 B <data.frame [150 × 5]>


      I want to rbind (or equivalent) all data with a common df_name. I'd like the output to be a named list. I can do this with tapply:



      desired = tapply(df$data, df$df_name, function(y) do.call(rbind,y)) 

      List of 2
      $ A:'data.frame': 450 obs. of 5 variables:
      ..$ Sepal.Length: num [1:450] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
      ..$ Sepal.Width : num [1:450] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
      ..$ Petal.Length: num [1:450] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
      ..$ Petal.Width : num [1:450] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
      ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
      $ B:'data.frame': 300 obs. of 5 variables:
      ..$ Sepal.Length: num [1:300] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
      ..$ Sepal.Width : num [1:300] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
      ..$ Petal.Length: num [1:300] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
      ..$ Petal.Width : num [1:300] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
      ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
      - attr(*, "dim")= int 2
      - attr(*, "dimnames")=List of 1
      ..$ : chr [1:2] "A" "B"


      I can't figure out how to do the same with purrr verbs. I think perhaps I need to start by setting the list names:



      df_p <- 
      df %>%
      mutate(data = setNames(data, df_name))


      I found this question but I can't figure out how to apply in this situation.










      share|improve this question














      After using purrr and friends to read in a load of csvs I have ended up with a tibble that looks something like this:



      library(tidyverse)

      df <-
      tibble(
      df_name = c("A", "B", "A", "A", "B"),
      data = list(iris)
      )

      df

      # A tibble: 5 x 2
      df_name data
      <chr> <list>
      1 A <data.frame [150 × 5]>
      2 B <data.frame [150 × 5]>
      3 A <data.frame [150 × 5]>
      4 A <data.frame [150 × 5]>
      5 B <data.frame [150 × 5]>


      I want to rbind (or equivalent) all data with a common df_name. I'd like the output to be a named list. I can do this with tapply:



      desired = tapply(df$data, df$df_name, function(y) do.call(rbind,y)) 

      List of 2
      $ A:'data.frame': 450 obs. of 5 variables:
      ..$ Sepal.Length: num [1:450] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
      ..$ Sepal.Width : num [1:450] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
      ..$ Petal.Length: num [1:450] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
      ..$ Petal.Width : num [1:450] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
      ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
      $ B:'data.frame': 300 obs. of 5 variables:
      ..$ Sepal.Length: num [1:300] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
      ..$ Sepal.Width : num [1:300] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
      ..$ Petal.Length: num [1:300] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
      ..$ Petal.Width : num [1:300] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
      ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
      - attr(*, "dim")= int 2
      - attr(*, "dimnames")=List of 1
      ..$ : chr [1:2] "A" "B"


      I can't figure out how to do the same with purrr verbs. I think perhaps I need to start by setting the list names:



      df_p <- 
      df %>%
      mutate(data = setNames(data, df_name))


      I found this question but I can't figure out how to apply in this situation.







      r purrr






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 9:21









      Pete900Pete900

      8951 gold badge10 silver badges26 bronze badges




      8951 gold badge10 silver badges26 bronze badges

























          3 Answers
          3






          active

          oldest

          votes


















          3














          We can use tidyr::unnest



          library(tidyverse)
          df %>% split(.$df_name) %>% map(.%>%unnest() %>% select(-df_name))

          #OR
          df %>% split(.$df_name) %>% map(~unnest(.) %>% select(-df_name))
          df %>% unnest(data) %>% split(.$df_name)


          As @kath pointed out that we can use unnest directly



          df %>% split(.$df_name) %>% map(unnest) 





          share|improve this answer






















          • 2





            map(unnest) would also work (to add another option ;) )

            – kath
            Mar 27 at 9:47


















          2














          You can use reduce from purrr and bind_rows (similar to rbind) from dplyr.



          df_list <- df %>% 
          group_by(df_name) %>%
          summarize(data = list(reduce(data, bind_rows)))

          df_list
          # A tibble: 2 x 2
          # df_name data
          # <chr> <list>
          # 1 A <data.frame [450 x 5]>
          # 2 B <data.frame [300 x 5]>


          For the exact same structure as in your tapply-version we would need to add the following:



          df_list2 <- df_list %>% 
          split(.$df_name) %>%
          map(~ .x$data[[1]])

          str(df_list2)
          List of 2
          $ A:'data.frame': 450 obs. of 5 variables:
          ..$ Sepal.Length: num [1:450] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
          ..$ Sepal.Width : num [1:450] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
          ..$ Petal.Length: num [1:450] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
          ..$ Petal.Width : num [1:450] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
          ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
          $ B:'data.frame': 300 obs. of 5 variables:
          ..$ Sepal.Length: num [1:300] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
          ..$ Sepal.Width : num [1:300] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
          ..$ Petal.Length: num [1:300] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
          ..$ Petal.Width : num [1:300] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
          ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...





          share|improve this answer



























          • Both great answers so thank you!

            – Pete900
            Mar 27 at 9:43



















          1














          I would use unnest and group_split :



          df %>% unnest(data) %>% group_split(df_name)

          # [[1]]
          # # A tibble: 450 x 6
          # df_name Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          # <chr> <dbl> <dbl> <dbl> <dbl> <fct>
          # 1 A 5.1 3.5 1.4 0.2 setosa
          # 2 A 4.9 3 1.4 0.2 setosa
          # 3 A 4.7 3.2 1.3 0.2 setosa
          # 4 A 4.6 3.1 1.5 0.2 setosa
          # 5 A 5 3.6 1.4 0.2 setosa
          # 6 A 5.4 3.9 1.7 0.4 setosa
          # 7 A 4.6 3.4 1.4 0.3 setosa
          # 8 A 5 3.4 1.5 0.2 setosa
          # 9 A 4.4 2.9 1.4 0.2 setosa
          # 10 A 4.9 3.1 1.5 0.1 setosa
          # # ... with 440 more rows
          #
          # [[2]]
          # # A tibble: 300 x 6
          # df_name Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          # <chr> <dbl> <dbl> <dbl> <dbl> <fct>
          # 1 B 5.1 3.5 1.4 0.2 setosa
          # 2 B 4.9 3 1.4 0.2 setosa
          # 3 B 4.7 3.2 1.3 0.2 setosa
          # 4 B 4.6 3.1 1.5 0.2 setosa
          # 5 B 5 3.6 1.4 0.2 setosa
          # 6 B 5.4 3.9 1.7 0.4 setosa
          # 7 B 4.6 3.4 1.4 0.3 setosa
          # 8 B 5 3.4 1.5 0.2 setosa
          # 9 B 4.4 2.9 1.4 0.2 setosa
          # 10 B 4.9 3.1 1.5 0.1 setosa
          # # ... with 290 more rows





          share|improve this answer

























          • Thanks, I hadn't heard of group_split before.

            – Pete900
            Mar 27 at 11:09











          • It's fairly new :) tidyverse.org/articles/2019/02/dplyr-0-8-0

            – Moody_Mudskipper
            Mar 27 at 11:15











          • Although as-is the resulting list elements are not named but I can see how to set them.

            – Pete900
            Mar 27 at 11:17






          • 1





            fair point, actually the help says "it does not name the elements of the list based on the grouping as this typically loses information and is confusing.", which I think is a mistake and some users complained about it in the github issues already. The function is still marked as experimental so hopefully they'll change it.

            – Moody_Mudskipper
            Mar 27 at 11:27













          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55373640%2fpurrr-rbind-list-of-data-frames-per-group%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          We can use tidyr::unnest



          library(tidyverse)
          df %>% split(.$df_name) %>% map(.%>%unnest() %>% select(-df_name))

          #OR
          df %>% split(.$df_name) %>% map(~unnest(.) %>% select(-df_name))
          df %>% unnest(data) %>% split(.$df_name)


          As @kath pointed out that we can use unnest directly



          df %>% split(.$df_name) %>% map(unnest) 





          share|improve this answer






















          • 2





            map(unnest) would also work (to add another option ;) )

            – kath
            Mar 27 at 9:47















          3














          We can use tidyr::unnest



          library(tidyverse)
          df %>% split(.$df_name) %>% map(.%>%unnest() %>% select(-df_name))

          #OR
          df %>% split(.$df_name) %>% map(~unnest(.) %>% select(-df_name))
          df %>% unnest(data) %>% split(.$df_name)


          As @kath pointed out that we can use unnest directly



          df %>% split(.$df_name) %>% map(unnest) 





          share|improve this answer






















          • 2





            map(unnest) would also work (to add another option ;) )

            – kath
            Mar 27 at 9:47













          3












          3








          3







          We can use tidyr::unnest



          library(tidyverse)
          df %>% split(.$df_name) %>% map(.%>%unnest() %>% select(-df_name))

          #OR
          df %>% split(.$df_name) %>% map(~unnest(.) %>% select(-df_name))
          df %>% unnest(data) %>% split(.$df_name)


          As @kath pointed out that we can use unnest directly



          df %>% split(.$df_name) %>% map(unnest) 





          share|improve this answer















          We can use tidyr::unnest



          library(tidyverse)
          df %>% split(.$df_name) %>% map(.%>%unnest() %>% select(-df_name))

          #OR
          df %>% split(.$df_name) %>% map(~unnest(.) %>% select(-df_name))
          df %>% unnest(data) %>% split(.$df_name)


          As @kath pointed out that we can use unnest directly



          df %>% split(.$df_name) %>% map(unnest) 






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 27 at 9:53

























          answered Mar 27 at 9:36









          A. SulimanA. Suliman

          7,4734 gold badges13 silver badges26 bronze badges




          7,4734 gold badges13 silver badges26 bronze badges










          • 2





            map(unnest) would also work (to add another option ;) )

            – kath
            Mar 27 at 9:47












          • 2





            map(unnest) would also work (to add another option ;) )

            – kath
            Mar 27 at 9:47







          2




          2





          map(unnest) would also work (to add another option ;) )

          – kath
          Mar 27 at 9:47





          map(unnest) would also work (to add another option ;) )

          – kath
          Mar 27 at 9:47













          2














          You can use reduce from purrr and bind_rows (similar to rbind) from dplyr.



          df_list <- df %>% 
          group_by(df_name) %>%
          summarize(data = list(reduce(data, bind_rows)))

          df_list
          # A tibble: 2 x 2
          # df_name data
          # <chr> <list>
          # 1 A <data.frame [450 x 5]>
          # 2 B <data.frame [300 x 5]>


          For the exact same structure as in your tapply-version we would need to add the following:



          df_list2 <- df_list %>% 
          split(.$df_name) %>%
          map(~ .x$data[[1]])

          str(df_list2)
          List of 2
          $ A:'data.frame': 450 obs. of 5 variables:
          ..$ Sepal.Length: num [1:450] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
          ..$ Sepal.Width : num [1:450] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
          ..$ Petal.Length: num [1:450] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
          ..$ Petal.Width : num [1:450] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
          ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
          $ B:'data.frame': 300 obs. of 5 variables:
          ..$ Sepal.Length: num [1:300] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
          ..$ Sepal.Width : num [1:300] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
          ..$ Petal.Length: num [1:300] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
          ..$ Petal.Width : num [1:300] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
          ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...





          share|improve this answer



























          • Both great answers so thank you!

            – Pete900
            Mar 27 at 9:43
















          2














          You can use reduce from purrr and bind_rows (similar to rbind) from dplyr.



          df_list <- df %>% 
          group_by(df_name) %>%
          summarize(data = list(reduce(data, bind_rows)))

          df_list
          # A tibble: 2 x 2
          # df_name data
          # <chr> <list>
          # 1 A <data.frame [450 x 5]>
          # 2 B <data.frame [300 x 5]>


          For the exact same structure as in your tapply-version we would need to add the following:



          df_list2 <- df_list %>% 
          split(.$df_name) %>%
          map(~ .x$data[[1]])

          str(df_list2)
          List of 2
          $ A:'data.frame': 450 obs. of 5 variables:
          ..$ Sepal.Length: num [1:450] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
          ..$ Sepal.Width : num [1:450] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
          ..$ Petal.Length: num [1:450] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
          ..$ Petal.Width : num [1:450] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
          ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
          $ B:'data.frame': 300 obs. of 5 variables:
          ..$ Sepal.Length: num [1:300] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
          ..$ Sepal.Width : num [1:300] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
          ..$ Petal.Length: num [1:300] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
          ..$ Petal.Width : num [1:300] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
          ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...





          share|improve this answer



























          • Both great answers so thank you!

            – Pete900
            Mar 27 at 9:43














          2












          2








          2







          You can use reduce from purrr and bind_rows (similar to rbind) from dplyr.



          df_list <- df %>% 
          group_by(df_name) %>%
          summarize(data = list(reduce(data, bind_rows)))

          df_list
          # A tibble: 2 x 2
          # df_name data
          # <chr> <list>
          # 1 A <data.frame [450 x 5]>
          # 2 B <data.frame [300 x 5]>


          For the exact same structure as in your tapply-version we would need to add the following:



          df_list2 <- df_list %>% 
          split(.$df_name) %>%
          map(~ .x$data[[1]])

          str(df_list2)
          List of 2
          $ A:'data.frame': 450 obs. of 5 variables:
          ..$ Sepal.Length: num [1:450] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
          ..$ Sepal.Width : num [1:450] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
          ..$ Petal.Length: num [1:450] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
          ..$ Petal.Width : num [1:450] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
          ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
          $ B:'data.frame': 300 obs. of 5 variables:
          ..$ Sepal.Length: num [1:300] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
          ..$ Sepal.Width : num [1:300] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
          ..$ Petal.Length: num [1:300] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
          ..$ Petal.Width : num [1:300] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
          ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...





          share|improve this answer















          You can use reduce from purrr and bind_rows (similar to rbind) from dplyr.



          df_list <- df %>% 
          group_by(df_name) %>%
          summarize(data = list(reduce(data, bind_rows)))

          df_list
          # A tibble: 2 x 2
          # df_name data
          # <chr> <list>
          # 1 A <data.frame [450 x 5]>
          # 2 B <data.frame [300 x 5]>


          For the exact same structure as in your tapply-version we would need to add the following:



          df_list2 <- df_list %>% 
          split(.$df_name) %>%
          map(~ .x$data[[1]])

          str(df_list2)
          List of 2
          $ A:'data.frame': 450 obs. of 5 variables:
          ..$ Sepal.Length: num [1:450] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
          ..$ Sepal.Width : num [1:450] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
          ..$ Petal.Length: num [1:450] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
          ..$ Petal.Width : num [1:450] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
          ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
          $ B:'data.frame': 300 obs. of 5 variables:
          ..$ Sepal.Length: num [1:300] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
          ..$ Sepal.Width : num [1:300] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
          ..$ Petal.Length: num [1:300] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
          ..$ Petal.Width : num [1:300] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
          ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 27 at 9:57

























          answered Mar 27 at 9:40









          kathkath

          5,70411 silver badges27 bronze badges




          5,70411 silver badges27 bronze badges















          • Both great answers so thank you!

            – Pete900
            Mar 27 at 9:43


















          • Both great answers so thank you!

            – Pete900
            Mar 27 at 9:43

















          Both great answers so thank you!

          – Pete900
          Mar 27 at 9:43






          Both great answers so thank you!

          – Pete900
          Mar 27 at 9:43












          1














          I would use unnest and group_split :



          df %>% unnest(data) %>% group_split(df_name)

          # [[1]]
          # # A tibble: 450 x 6
          # df_name Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          # <chr> <dbl> <dbl> <dbl> <dbl> <fct>
          # 1 A 5.1 3.5 1.4 0.2 setosa
          # 2 A 4.9 3 1.4 0.2 setosa
          # 3 A 4.7 3.2 1.3 0.2 setosa
          # 4 A 4.6 3.1 1.5 0.2 setosa
          # 5 A 5 3.6 1.4 0.2 setosa
          # 6 A 5.4 3.9 1.7 0.4 setosa
          # 7 A 4.6 3.4 1.4 0.3 setosa
          # 8 A 5 3.4 1.5 0.2 setosa
          # 9 A 4.4 2.9 1.4 0.2 setosa
          # 10 A 4.9 3.1 1.5 0.1 setosa
          # # ... with 440 more rows
          #
          # [[2]]
          # # A tibble: 300 x 6
          # df_name Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          # <chr> <dbl> <dbl> <dbl> <dbl> <fct>
          # 1 B 5.1 3.5 1.4 0.2 setosa
          # 2 B 4.9 3 1.4 0.2 setosa
          # 3 B 4.7 3.2 1.3 0.2 setosa
          # 4 B 4.6 3.1 1.5 0.2 setosa
          # 5 B 5 3.6 1.4 0.2 setosa
          # 6 B 5.4 3.9 1.7 0.4 setosa
          # 7 B 4.6 3.4 1.4 0.3 setosa
          # 8 B 5 3.4 1.5 0.2 setosa
          # 9 B 4.4 2.9 1.4 0.2 setosa
          # 10 B 4.9 3.1 1.5 0.1 setosa
          # # ... with 290 more rows





          share|improve this answer

























          • Thanks, I hadn't heard of group_split before.

            – Pete900
            Mar 27 at 11:09











          • It's fairly new :) tidyverse.org/articles/2019/02/dplyr-0-8-0

            – Moody_Mudskipper
            Mar 27 at 11:15











          • Although as-is the resulting list elements are not named but I can see how to set them.

            – Pete900
            Mar 27 at 11:17






          • 1





            fair point, actually the help says "it does not name the elements of the list based on the grouping as this typically loses information and is confusing.", which I think is a mistake and some users complained about it in the github issues already. The function is still marked as experimental so hopefully they'll change it.

            – Moody_Mudskipper
            Mar 27 at 11:27















          1














          I would use unnest and group_split :



          df %>% unnest(data) %>% group_split(df_name)

          # [[1]]
          # # A tibble: 450 x 6
          # df_name Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          # <chr> <dbl> <dbl> <dbl> <dbl> <fct>
          # 1 A 5.1 3.5 1.4 0.2 setosa
          # 2 A 4.9 3 1.4 0.2 setosa
          # 3 A 4.7 3.2 1.3 0.2 setosa
          # 4 A 4.6 3.1 1.5 0.2 setosa
          # 5 A 5 3.6 1.4 0.2 setosa
          # 6 A 5.4 3.9 1.7 0.4 setosa
          # 7 A 4.6 3.4 1.4 0.3 setosa
          # 8 A 5 3.4 1.5 0.2 setosa
          # 9 A 4.4 2.9 1.4 0.2 setosa
          # 10 A 4.9 3.1 1.5 0.1 setosa
          # # ... with 440 more rows
          #
          # [[2]]
          # # A tibble: 300 x 6
          # df_name Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          # <chr> <dbl> <dbl> <dbl> <dbl> <fct>
          # 1 B 5.1 3.5 1.4 0.2 setosa
          # 2 B 4.9 3 1.4 0.2 setosa
          # 3 B 4.7 3.2 1.3 0.2 setosa
          # 4 B 4.6 3.1 1.5 0.2 setosa
          # 5 B 5 3.6 1.4 0.2 setosa
          # 6 B 5.4 3.9 1.7 0.4 setosa
          # 7 B 4.6 3.4 1.4 0.3 setosa
          # 8 B 5 3.4 1.5 0.2 setosa
          # 9 B 4.4 2.9 1.4 0.2 setosa
          # 10 B 4.9 3.1 1.5 0.1 setosa
          # # ... with 290 more rows





          share|improve this answer

























          • Thanks, I hadn't heard of group_split before.

            – Pete900
            Mar 27 at 11:09











          • It's fairly new :) tidyverse.org/articles/2019/02/dplyr-0-8-0

            – Moody_Mudskipper
            Mar 27 at 11:15











          • Although as-is the resulting list elements are not named but I can see how to set them.

            – Pete900
            Mar 27 at 11:17






          • 1





            fair point, actually the help says "it does not name the elements of the list based on the grouping as this typically loses information and is confusing.", which I think is a mistake and some users complained about it in the github issues already. The function is still marked as experimental so hopefully they'll change it.

            – Moody_Mudskipper
            Mar 27 at 11:27













          1












          1








          1







          I would use unnest and group_split :



          df %>% unnest(data) %>% group_split(df_name)

          # [[1]]
          # # A tibble: 450 x 6
          # df_name Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          # <chr> <dbl> <dbl> <dbl> <dbl> <fct>
          # 1 A 5.1 3.5 1.4 0.2 setosa
          # 2 A 4.9 3 1.4 0.2 setosa
          # 3 A 4.7 3.2 1.3 0.2 setosa
          # 4 A 4.6 3.1 1.5 0.2 setosa
          # 5 A 5 3.6 1.4 0.2 setosa
          # 6 A 5.4 3.9 1.7 0.4 setosa
          # 7 A 4.6 3.4 1.4 0.3 setosa
          # 8 A 5 3.4 1.5 0.2 setosa
          # 9 A 4.4 2.9 1.4 0.2 setosa
          # 10 A 4.9 3.1 1.5 0.1 setosa
          # # ... with 440 more rows
          #
          # [[2]]
          # # A tibble: 300 x 6
          # df_name Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          # <chr> <dbl> <dbl> <dbl> <dbl> <fct>
          # 1 B 5.1 3.5 1.4 0.2 setosa
          # 2 B 4.9 3 1.4 0.2 setosa
          # 3 B 4.7 3.2 1.3 0.2 setosa
          # 4 B 4.6 3.1 1.5 0.2 setosa
          # 5 B 5 3.6 1.4 0.2 setosa
          # 6 B 5.4 3.9 1.7 0.4 setosa
          # 7 B 4.6 3.4 1.4 0.3 setosa
          # 8 B 5 3.4 1.5 0.2 setosa
          # 9 B 4.4 2.9 1.4 0.2 setosa
          # 10 B 4.9 3.1 1.5 0.1 setosa
          # # ... with 290 more rows





          share|improve this answer













          I would use unnest and group_split :



          df %>% unnest(data) %>% group_split(df_name)

          # [[1]]
          # # A tibble: 450 x 6
          # df_name Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          # <chr> <dbl> <dbl> <dbl> <dbl> <fct>
          # 1 A 5.1 3.5 1.4 0.2 setosa
          # 2 A 4.9 3 1.4 0.2 setosa
          # 3 A 4.7 3.2 1.3 0.2 setosa
          # 4 A 4.6 3.1 1.5 0.2 setosa
          # 5 A 5 3.6 1.4 0.2 setosa
          # 6 A 5.4 3.9 1.7 0.4 setosa
          # 7 A 4.6 3.4 1.4 0.3 setosa
          # 8 A 5 3.4 1.5 0.2 setosa
          # 9 A 4.4 2.9 1.4 0.2 setosa
          # 10 A 4.9 3.1 1.5 0.1 setosa
          # # ... with 440 more rows
          #
          # [[2]]
          # # A tibble: 300 x 6
          # df_name Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          # <chr> <dbl> <dbl> <dbl> <dbl> <fct>
          # 1 B 5.1 3.5 1.4 0.2 setosa
          # 2 B 4.9 3 1.4 0.2 setosa
          # 3 B 4.7 3.2 1.3 0.2 setosa
          # 4 B 4.6 3.1 1.5 0.2 setosa
          # 5 B 5 3.6 1.4 0.2 setosa
          # 6 B 5.4 3.9 1.7 0.4 setosa
          # 7 B 4.6 3.4 1.4 0.3 setosa
          # 8 B 5 3.4 1.5 0.2 setosa
          # 9 B 4.4 2.9 1.4 0.2 setosa
          # 10 B 4.9 3.1 1.5 0.1 setosa
          # # ... with 290 more rows






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 10:31









          Moody_MudskipperMoody_Mudskipper

          27.1k4 gold badges48 silver badges81 bronze badges




          27.1k4 gold badges48 silver badges81 bronze badges















          • Thanks, I hadn't heard of group_split before.

            – Pete900
            Mar 27 at 11:09











          • It's fairly new :) tidyverse.org/articles/2019/02/dplyr-0-8-0

            – Moody_Mudskipper
            Mar 27 at 11:15











          • Although as-is the resulting list elements are not named but I can see how to set them.

            – Pete900
            Mar 27 at 11:17






          • 1





            fair point, actually the help says "it does not name the elements of the list based on the grouping as this typically loses information and is confusing.", which I think is a mistake and some users complained about it in the github issues already. The function is still marked as experimental so hopefully they'll change it.

            – Moody_Mudskipper
            Mar 27 at 11:27

















          • Thanks, I hadn't heard of group_split before.

            – Pete900
            Mar 27 at 11:09











          • It's fairly new :) tidyverse.org/articles/2019/02/dplyr-0-8-0

            – Moody_Mudskipper
            Mar 27 at 11:15











          • Although as-is the resulting list elements are not named but I can see how to set them.

            – Pete900
            Mar 27 at 11:17






          • 1





            fair point, actually the help says "it does not name the elements of the list based on the grouping as this typically loses information and is confusing.", which I think is a mistake and some users complained about it in the github issues already. The function is still marked as experimental so hopefully they'll change it.

            – Moody_Mudskipper
            Mar 27 at 11:27
















          Thanks, I hadn't heard of group_split before.

          – Pete900
          Mar 27 at 11:09





          Thanks, I hadn't heard of group_split before.

          – Pete900
          Mar 27 at 11:09













          It's fairly new :) tidyverse.org/articles/2019/02/dplyr-0-8-0

          – Moody_Mudskipper
          Mar 27 at 11:15





          It's fairly new :) tidyverse.org/articles/2019/02/dplyr-0-8-0

          – Moody_Mudskipper
          Mar 27 at 11:15













          Although as-is the resulting list elements are not named but I can see how to set them.

          – Pete900
          Mar 27 at 11:17





          Although as-is the resulting list elements are not named but I can see how to set them.

          – Pete900
          Mar 27 at 11:17




          1




          1





          fair point, actually the help says "it does not name the elements of the list based on the grouping as this typically loses information and is confusing.", which I think is a mistake and some users complained about it in the github issues already. The function is still marked as experimental so hopefully they'll change it.

          – Moody_Mudskipper
          Mar 27 at 11:27





          fair point, actually the help says "it does not name the elements of the list based on the grouping as this typically loses information and is confusing.", which I think is a mistake and some users complained about it in the github issues already. The function is still marked as experimental so hopefully they'll change it.

          – Moody_Mudskipper
          Mar 27 at 11:27

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55373640%2fpurrr-rbind-list-of-data-frames-per-group%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript