Method 2 : Using list() method
The data frame columns can be explicitly mapped to lists using the list() method in R. As a result of this a generic list object with a custom name can be specified within the aggregate function usage.
Syntax:
list(new-col-name = df$old-col-name)
Example: Set column names with aggregate function
R
# creating a data frame data_frame <- data.frame (col1 = c (1:9), col2 = LETTERS [1:3]) print ( "Original DataFrame" ) print (data_frame) # using aggregate method data_mod <- aggregate ( list (mean = data_frame$col1), list (letter = data_frame$col2), mean) # printing the modified dataframe print ( "Modified DataFrame" ) print (data_mod) |
Output
[1] "Original DataFrame" col1 col2 1 1 A 2 2 B 3 3 C 4 4 A 5 5 B 6 6 C 7 7 A 8 8 B 9 9 C [1] "Modified DataFrame" letter mean 1 A 4 2 B 5 3 C 6
How to Set Column Names within the aggregate Function in R
In this article we will discuss how to set column names with the aggregate function in R programming language.
The aggregate method in base R is used to divide the data frame into smaller subsets and compute a summary statistics for each of the formed groups. The function to be applied can be sum, mean, mode or any of the pre-defined aggregate methods available. This method gives a better clarity about the data.
Syntax: aggregate(formula, data, function)
Parameters:
- formula: the variable(s) of the input data frame we want to apply functions on.
- data: the data that we want to use for group by operation.
- function: the function or calculation to be applied.