I have an extensive block of code that I wrote using the dplyr syntax in R. However, I am trying to put this code in a loop to end up creating multiple output files, not just one. Unfortunately, I cannot do this.
To illustrate my problem, let me refer to the commonly used "iris" dataset in R:
> data("iris")
> str(iris)
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num
$ Sepal.Width : num
$ Petal.Length: num
$ Petal.Width : num
$ Species : Factor w/ 3 levels "setosa","versicolor","virginica"
Say I want to keep the middle petal. The length of the species is versicolor. The dplyr code might look like this:
MeanLength2 <- iris %>% filter(Species=="versicolor")
%>% summarize(mean(Petal.Length)) %>% print()
Which will give the following value:
mean(Petal.Length)
1 4.26
Let's try to create a loop to get the average length of the petals for all species.
From the fact that I know little about loops, I would like to do something like this:
for (i in unique(iris$Species))
{
iris %>% filter(iris$Species==unique(iris$Species)[i]) %>%
summarize(mean(iris$Petal.Length)) %>% print()
print(i)
}
- , dplyr. , .
:
mean(iris$Petal.Length)
1 3.758
[1] "setosa"
mean(iris$Petal.Length)
1 3.758
[1] "versicolor"
mean(iris$Petal.Length)
1 3.758
[1] "virginica"
, 3.758 , . , "" . , , , , , .
- ? , , , , , "group_by" dplyr, 100 PDF , , , , .