How to usegroup_by() and summarise() method in R Language
The data transformation in R group_by() and summarise() methods are used collectively to group by variables of the data frame and reduce multiple values down to a single value. It is used to make the data more readable. The column name can be specified in R’s group_by() method. The data can be arranged in groups and then further summarised using the base aggregate methods in this package.
Syntax: group_by(col-name)
Syntax: group_by(col,..) %>% summarise(action)
The data in the data frame are grouped according to the col3 value. The count column indicates the number of records in each group; for instance, there are five rows with col3 = 0. The mean is then calculated for all the elements in a. particular group.
R
# Importing dplyr library (dplyr) # Creating a data frame data_frame = data.frame ( col1 = c (2,4,1,7,5,3,5,8), col2 = letters [1:8], col3 = c (0,1,1,1,0,0,0,0), col4 = c (9:16)) print ( "Data Frame" ) print (data_frame) # Mutate data using group_by() # and summarise() data_frame_mutate <- data_frame %>% group_by (col3) %>% summarise ( count = n (), mean_col1 = mean (col1) ) print ( "Mutated Data Frame" ) print (data_frame_mutate) |
Output:
col1 col2 col3 col4
1 2 a 0 9
2 4 b 1 10
3 1 c 1 11
4 7 d 1 12
5 5 e 0 13
6 3 f 0 14
7 5 g 0 15
8 8 h 0 16
[1] "Mutated Data Frame"
# A tibble: 2 x 3
col3 count mean_col1
<dbl> <int> <dbl>
1 0 5 4.6
2 1 3 4
How to Transform Data in R?
In this article, we will learn how to transform data in the R programming language.
Contact Us