- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change the column names in R within aggregate function?
The column names in an R data frame are an important part of the data because by reading the column names any viewer is likely to understand the theoretical background behind it. If that name is not appropriate then we might want to change it. While using the aggregate function to calculate mean or any other statistical summary, it is possible to change that name with another name by defining the new name with list.
Example
Consider the below data frame −
set.seed(1) x1<-rnorm(100) x2<-rep(c(LETTERS[1:25]),times=4) df<-data.frame(x1,x2) head(df,20) x1 x2 1 -0.62645381 A 2 0.18364332 B 3 -0.83562861 C 4 1.59528080 D 5 0.32950777 E 6 -0.82046838 F 7 0.48742905 G 8 0.73832471 H 9 0.57578135 I 10 -0.30538839 J 11 1.51178117 K 12 0.38984324 L 13 -0.62124058 M 14 -2.21469989 N 15 1.12493092 O 16 -0.04493361 P 17 -0.01619026 Q 18 0.94383621 R 19 0.82122120 S 20 0.59390132 T tail(df,20) x1 x2 81 -0.5686687 F 82 -0.1351786 G 83 1.1780870 H 84 -1.5235668 I 85 0.5939462 J 86 0.3329504 K 87 1.0630998 L 88 -0.3041839 M 89 0.3700188 N 90 0.2670988 O 91 -0.5425200 P 92 1.2078678 Q 93 1.1604026 R 94 0.7002136 S 95 1.5868335 T 96 0.5584864 U 97 -1.2765922 V 98 -0.5732654 W 99 -1.2246126 X 100 -0.4734006 Y
Changing the name of columns inside aggregate function with mean −
new_df <- aggregate(list(One=df$x1),list(Two=df$x2),mean) head(new_df) Two One 1 A 0.001742391 2 B -0.256867612 3 C -0.491038988 4 D 0.015527244 5 E 0.397738022 6 F 0.487485583 new_df Two One 1 A 0.001742391 2 B -0.256867612 3 C -0.491038988 4 D 0.015527244 5 E 0.397738022 6 F 0.487485583 7 G -0.029439692 8 H 0.314987172 9 I -0.107967715 10 J -0.305889090 11 K 0.957838684 12 L 0.254853279 13 M -0.073749635 14 N -0.179163387 15 O 0.352983062 16 P -0.140796234 17 Q -0.216660692 18 R 1.066689266 19 S 0.557837845 20 T 0.916147688 21 U 0.311369542 22 V -0.209955094 23 W 0.220139712 24 X -1.065102039 25 Y -0.056525141
- Related Articles
- How to change the column names and row names of a data frame in R?
- How to change the repeated row names and column names to a sequence in a matrix in R?
- How to aggregate matrix columns by row names in R?
- How to convert a column values to column names in R?
- How to change column names to capital letters from lower case or vice versa in R?
- How to add suffix to column names in R?
- How to find the column names and row names from a matrix in R?
- How to remove the row names or column names from a matrix in R?
- How to add column values in MySQL without using aggregate function?
- How to convert a row into column names in R?
- How to remove column names from an R data frame?
- How to remove repeated column names in a data.table object in R?
- Find the average of column values in MySQL using aggregate function
- How to match and replace column names stored in R data frames in R-Programming?
- How to change the axes labels using plot function in R?

Advertisements