How to Get the Percentage by Group in R (Example Code)
In this R programming tutorial you’ll learn how to return the percentage by group.
Construction of Exemplifying Data
data(iris) # Construct example data iris_new <- iris[c(1:3, 51:53, 101:103), c(1, 5)] iris_new$subgroup <- letters[1:3] iris_new # Sepal.Length Species subgroup # 1 5.1 setosa a # 2 4.9 setosa b # 3 4.7 setosa c # 51 7.0 versicolor a # 52 6.4 versicolor b # 53 6.9 versicolor c # 101 6.3 virginica a # 102 5.8 virginica b # 103 7.1 virginica c |
data(iris) # Construct example data iris_new <- iris[c(1:3, 51:53, 101:103), c(1, 5)] iris_new$subgroup <- letters[1:3] iris_new # Sepal.Length Species subgroup # 1 5.1 setosa a # 2 4.9 setosa b # 3 4.7 setosa c # 51 7.0 versicolor a # 52 6.4 versicolor b # 53 6.9 versicolor c # 101 6.3 virginica a # 102 5.8 virginica b # 103 7.1 virginica c
Example: Get Percentage by Group Using dplyr Package
install.packages("dplyr") # Install & load dplyr package library("dplyr") |
install.packages("dplyr") # Install & load dplyr package library("dplyr")
iris_new %>% # Calculating percentage by group group_by(Species) %>% mutate(perc = Sepal.Length / sum(Sepal.Length)) # # A tibble: 9 x 4 # # Groups: Species [3] # Sepal.Length Species subgroup perc # <dbl> <fct> <chr> <dbl> # 1 5.1 setosa a 0.347 # 2 4.9 setosa b 0.333 # 3 4.7 setosa c 0.320 # 4 7 versicolor a 0.345 # 5 6.4 versicolor b 0.315 # 6 6.9 versicolor c 0.340 # 7 6.3 virginica a 0.328 # 8 5.8 virginica b 0.302 # 9 7.1 virginica c 0.370 |
iris_new %>% # Calculating percentage by group group_by(Species) %>% mutate(perc = Sepal.Length / sum(Sepal.Length)) # # A tibble: 9 x 4 # # Groups: Species [3] # Sepal.Length Species subgroup perc # <dbl> <fct> <chr> <dbl> # 1 5.1 setosa a 0.347 # 2 4.9 setosa b 0.333 # 3 4.7 setosa c 0.320 # 4 7 versicolor a 0.345 # 5 6.4 versicolor b 0.315 # 6 6.9 versicolor c 0.340 # 7 6.3 virginica a 0.328 # 8 5.8 virginica b 0.302 # 9 7.1 virginica c 0.370