How to Set Column Names within the aggregate Function in R
Last Updated :
14 Sep, 2021
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 <- 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
Similar Reads
Set Column Names when Using cbind Function in R In this article, we are going to see how to set the column names when using cbind() function in R Programming language. Let's create and combine two vectors for demonstration: R # create two vectors with integer and string types vector1 = c(1,2,3,4,5) vector2 = c("sravan","bobby", "ojsawi","gnanesh"
2 min read
How to Aggregate Multiple Columns in R? In this article, we will discuss how to aggregate multiple columns in R Programming Language. Aggregation means combining two or more data. Here we are going to use the aggregate function to get the summary statistics for one or more variables in a data frame. Syntax: aggregate(sum_column ~ group_co
3 min read
How to add a prefix to column names in R DataFrame ? In this article, we will discuss how to add prefixes to column names in DataFrame in R Programming Language. Dataset in use: First SecondThird1a72ab83cv94dsd10Method 1 : Using paste() method In order to modify the column names, the paste function in R can be used. The paste() method, can be used for
4 min read
How to add suffix to column names in R DataFrame ? Each of the columns in a data frame is defined by a name, known as the column name. It may be of the type of numerical or string value. In this article, we will discuss how to add a suffix to column names in DataFrame in R Programming Language. Method 1 : Using paste() method In order to modify the
4 min read
How to assign column names based on existing row in R DataFrame ? In this article, we will discuss how assign column names or headers to a DataFrame based on rows in R Programming Language. Method 1 : Using as.character() method unlist() method in R is used to simulate the conversion of a list to vector. It simplifies to produce a vector by preserving all componen
3 min read