How to use summarise_at() method In R Language
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 :
- data – The data frame to summarise the columns of
- function – The function to apply on all the data frame columns.
R
library ( "dplyr" ) # creating a data frame df < - data.frame (col1= sample ( rep ( c (1: 5), each=3)), col2=1: 15, col3= letters [1:15]) print ( "original dataframe" ) print (df) # summarising the data print ( "summarised dataframe" ) df % >% summarise_at ( c ( "col1" , "col2" ), mean, na.rm= TRUE ) |
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
Summarise multiple columns using dplyr in R
In this article, we will discuss how to summarise multiple columns using dplyr package in R Programming Language,