I am trying to calculate the non zero minimum of a column within groups using dplyr
. I have seen a few similar questions around, however, none seem to work. Let's see an example with iris
. I would like to create a new column :min_length, with the minimum pedal length higher than 1 for each species (I did not use 0 for this example to fit the dataset). I tried:
iris <- iris %>% group_by(Species) %>% mutate(min_length = min(Petal.Length>1))
But I get:
> iris$min_length
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
[58] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[115] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Also, I tried an answer suggested in Find minimum non-zero value in a column R :
iris$min_length <- iris %>% group_by(Species) %>% min(iris[iris$Sepal.Length>1, "Sepal.Length"])
But I get:
Error in FUN(X[[i]], ...) : only defined on a data frame with all numeric variables
The intended output would be:
iris$min_length
[1] 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1
[29] 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 3.0 3.0 3.0 3.0 3.0 3.0
[57] 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0
[85] 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5
[113] 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5
[141] 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5
Any ideas?
You almost got it
library(tidyverse)
iris %>% group_by(Species) %>% mutate(min_length = min(Petal.Length[Petal.Length > 1]))