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

后端 未结 2 500
没有蜡笔的小新
没有蜡笔的小新 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 07:47

    It is unfortunate that your code didn't raise any errors. If you run your code line by line you'll understand what I'm saying. For this example I will choose the first iteration of your loop, let's replace i for "setosa":

    > iris  %>% filter(iris$Species == unique(iris$Species)["setosa"])
    [1] Sepal.Length Sepal.Width  Petal.Length Petal.Width  Species     
    <0 rows> (or 0-length row.names)
    

    Your filter yields a data frame with no observations, so no point in going ahead, but for this example, let's run the rest of the code:

    > iris  %>% filter(iris$Species == unique(iris$Species)["setosa"]) %>%  
    + summarize(mean(iris$Petal.Length))
      mean(iris$Petal.Length)
    1                   3.758
    

    What happened is that you're calling the iris dataset from within your code, a more obvious example would be:

    > filter(iris, iris$Species == unique(iris$Species)["setosa"]) %>% 
    + summarize(mean(mtcars$cyl))
      mean(mtcars$cyl)
    1           6.1875
    

    That's why you don't get the answer you expected, your filter didn't work and you got a summary statistic from another dataset.

    As TJ Mahr mentioned, your code without specifying the dataset runs fine:

    > for (i in unique(iris$Species))
    + {
    +     iris %>% filter(Species==i) %>%
    +         summarize(mean(Petal.Length)) %>% print()
    +     print(i) 
    + }
      mean(Petal.Length)
    1              1.462
    [1] "setosa"
      mean(Petal.Length)
    1               4.26
    [1] "versicolor"
      mean(Petal.Length)
    1              5.552
    [1] "virginica"
    

    I hope this helps

提交回复
热议问题