Skip to content
Related Articles

Related Articles

Improve Article

How to Set Column Names within the aggregate Function in R

  • Last Updated : 14 Sep, 2021
Geek Week

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. 

Method 1: Using setNames() method

The setNames() method is used to specify the name of an object and then return the object. In case of data frame, the columns can be renamed with new names, using the c() method. 

Syntax: setNames(data, col-name-vec)

Parameter : 



data – The data frame to be applied the function onto

col-name-vec – The column name vector containing the names of the columns. 

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_agg <- aggregate(col1 ~ col2, data_frame, sum)
  
# using setnames method
data_mod <- setNames(data_agg,  
                       c("C1", "C2"))
  
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" 
  C1 C2 
1  A 12 
2  B 15 
3  C 18

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 <- aggregatelist(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



My Personal Notes arrow_drop_up
Recommended Articles
Page :