How to Set Column Names within the aggregate Function in R Last Updated : 14 Sep, 2021 Comments Improve Suggest changes Like Article Like Report 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 18Method 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 Comment More infoAdvertise with us Next Article How to Set Column Names within the aggregate Function in R Y yashchuahan Follow Improve Article Tags : R Language R Programs R-DataFrame R DataFrame-Programs 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 Convert list to dataframe with specific column names in R A list contains different types of objects as their components. The components may belong to different data types or different dimensions. Vector can be useful components of a list and can be easily mapped as the rows or columns of a dataframe. Each column in the dataframe is referenced using a uniq 4 min read How to convert DataFrame column from Character to Numeric in R ? In this article, we will discuss how to convert DataFrame column from Character to Numeric in R Programming Language. All dataframe column is associated with a class which is an indicator of the data type to which the elements of that column belong to. Therefore, in order to simulate the data type c 5 min read Create DataFrame with Spaces in Column Names in R In this article, we will see how to create a DataFrame with spaces in column names in R Programming Language. Method 1: Using check.names attribute The data.frame() method in R can be used to create a data frame with individual rows and columns in R. This method contains an attribute check.names, wh 4 min read How to select multiple DataFrame columns by name in R ? In this article, we will discuss how to select multiple columns from a DataFrame by name in R Programming Language. To get multiple columns we will use the list data structure. By using a list we can pass the dataframe columns separated with a comma. Then, we can get list by using list() function Sy 1 min read How to find the sum of column values of an R dataframe? In this article, we are going to find the sum of the column values of a dataframe in R with the use of sum() function. Syntax: sum(dataframe$column_name) Creating a Dataframe A dataframe can be created with the use of data.frame() function that is pre-defined in the R library. This function accepts 2 min read Like