- Hands-On Data Science with R
- Vitor Bianchi Lanzetta Nataraj Dasgupta Ricardo Anjoleto Farias
- 138字
- 2025-02-15 11:19:08
Summarise
The summarise verb is used to obtain aggregate values, generally over a grouped variable.
The following highlights some of the common operations using summarise. Generally, summarise is preceded by a group_by operation, that is, the summary is performed over grouped variables, as shown:
# In the example below: # 1) We sorted the DataFrame by State Name using arrange # 2) We applied a group-by using Region, i.e., all resulting values would be aggregated using Region # 3) We calcuated the values for total rows using n(), the unique states belonging to each region using n_distinct # the max & mean literacy using max and mean respectively # tstate %>% arrange(Name) %>% group_by(Region) %>% summarise(total_rows = n(), first_state = first(Name), unique_states = n_distinct(Name), max_literacy = max(100-Illiteracy), mean_literacy = mean(100-Illiteracy, na.rm=T))
The output is as follows: