How to write loops “for” loops in R using dplyr syntax

后端 未结 2 505
没有蜡笔的小新
没有蜡笔的小新 2021-02-06 07:25

I have an extensive block of code that I\'ve written using dplyr syntax in R. However, I am trying to put that code in a loop, so that I can ultimately create multiple output fi

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-06 08:01

    As I mentioned in my comment, if you really need the results separated, it will probably be easier to just use group_by and then split() the results:

    iris %>% 
      group_by(Species) %>% 
      summarise(mn = mean(Petal.Length)) %>% 
      split(.,.$Species)
    
    $setosa
    # A tibble: 1 × 2
      Species    mn
        
    1  setosa 1.462
    
    $versicolor
    # A tibble: 1 × 2
         Species    mn
           
    1 versicolor  4.26
    
    $virginica
    # A tibble: 1 × 2
        Species    mn
          
    1 virginica 5.552
    

提交回复
热议问题