Hello team,
I am trying to create a new column based on a subset of data from another column.
As an example with the Iris data, how how would I calculate:
- The number of plants with a petal wide >0.2 for each species
- The number of plants with a petal wide <0.2 for each species
Then store this in a data frame.
I have worked out a 'long way round' of doing this but it feels quite clunky (see below)
Any help would be greatly appreciated!!
Many thanks
iris.big_petal <- iris %>%
filter(Petal.Length > 0.2) %>%
group_by(Species) %>%
mutate(density = n())
iris.small_petal <-iris %>%
filter(Petal.Length < 0.2) %>%
group_by(Species) %>%
mutate(density = n())
## and etc for the other species
iris.sum <- bind_rows(iris.small_petal,iris.big_petal)
```{r }