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
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
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
<fctr> <dbl>
1 setosa 1.462
$versicolor
# A tibble: 1 × 2
Species mn
<fctr> <dbl>
1 versicolor 4.26
$virginica
# A tibble: 1 × 2
Species mn
<fctr> <dbl>
1 virginica 5.552