![]() |
VOOZH | about |
In this article, we will discuss how to summarise multiple columns using dplyr package in R Programming Language,
The summarise_all method in R is used to affect every column of the data frame. The output data frame returns all the columns of the data frame where the specified function is applied over every column.
summarise_all(data, function)
Arguments :
Output
[1] "original dataframe" col1 col2 1 2 1 2 3 2 3 4 3 4 2 4 5 2 5 6 4 6 7 1 7 8 1 8 9 5 9 10 3 10 11 5 11 12 1 12 13 4 13 14 5 14 15 3 15 col1 col2 1 3 8
Explanation: The mean of all the values is calculated column-wise, that is, the sum of values of col1 is calculated and divided by the number of rows. Similarly, the summation of values is computed for col2 and col3. All the columns are returned in the final output.
The summarise_at() affects variables that are extracted with a character vector or vars(). It applies the selected function to the data frame. The output data frame contains all the columns that are specified in the summarise_at method. In case all the columns of the data frame are mentioned, then the functionality of this method is the same as the summarise_all method.
data %>% summarise_at(vars(-cols(), ...), function)
Arguments :
Output
[1] "original dataframe" col1 col2 col3 1 3 1 a 2 5 2 b 3 4 3 c 4 4 4 d 5 5 5 e 6 3 6 f 7 2 7 g 8 2 8 h 9 1 9 i 10 4 10 j 11 2 11 k 12 5 12 l 13 1 13 m 14 3 14 n 15 1 15 o [1] "summarised dataframe" col1 col2 1 3 8