How to Rename Multiple Columns in R

When you need to rename multiple columns in r there are two different formulas you can use. There is the names function, which is a base r function, and the rename function is from the dplyr package. Both functions will rename the columns in a data frame, but the rename function can save the new data frame to a new object, while the names function changes the original.

Description

When you rename multiple columns in r, there are two functions that you can use. They are, the names function, and the rename function. Both functions can be a little complicated depending upon how you are changing the data frame. The real question with regards to which one you want to use depends upon whether you want to change the original object or save the result to a different object. The basic format of the names function is names(dataframe) and the rename function has the format of rename(dataframe, new column name = old name, …). In both cases, the complexity arises when working with multiple column names.

Explanation

While the functions used to rename multiple columns in r will not allow you to add a new column, both functions do allow you to rename the columns in a data frame. The functions are simple enough once you understand them, however, the renames function is the easier of the two. Both functions successfully renamed the columns but there is a significant difference between the two. The names function will save the results to the original data set, but the renamed functioned will save it to a new data set or overwrite the old one. They are both useful tools, but the best one to use depends upon the situation.

Examples

Here we have three examples of the two functions used to rename multiple columns in r.

> z = data.frame(x1 = c(1, 2, 3, 4, 5),
+ x2 = c(6, 7, 8, 9, 10),
+ x3 = c(11,12,13,14,15))
> z
x1 x2 x3
1 1 6 11
2 2 7 12
3 3 8 13
4 4 9 14
5 5 10 15
> names(z)[names(z) == “x2”] = “y1”
> z
x1 y1 x3
1 1 6 11
2 2 7 12
3 3 8 13
4 4 9 14
5 5 10 15

In this example, we are changing the name of a specific column. This illustrates doing a single column name change with this method.

> z = data.frame(x1 = c(1, 2, 3, 4, 5),
+ x2 = c(6, 7, 8, 9, 10),
+ x3 = c(11,12,13,14,15))
> z
x1 x2 x3
1 1 6 11
2 2 7 12
3 3 8 13
4 4 9 14
5 5 10 15
> names(z) = c(‘y1’, ‘y2′,’y3’)
> z
y1 y2 y3
1 1 6 11
2 2 7 12
3 3 8 13
4 4 9 14
5 5 10 15

In this example, we are renaming multiple column names with the names function. This is the easiest way to use this method, you can change a partial set of column names by simply equating the new names to the old names when you do not want to change them.

> library(dplyr)
> z = data.frame(x1 = c(1, 2, 3, 4, 5),
+ x2 = c(6, 7, 8, 9, 10),
+ x3 = c(11,12,13,14,15))
> z
x1 x2 x3
1 1 6 11
2 2 7 12
3 3 8 13
4 4 9 14
5 5 10 15
> zz = rename(z, ‘y1’ = ‘x1’, ‘y2’ = ‘x2′,’y3’ = ‘x3’)
> zz
y1 y2 y3
1 1 6 11
2 2 7 12
3 3 8 13
4 4 9 14
5 5 10 15

In this example, we are using the rename function to rename multiple column names.

Application

There are many applications for renaming multiple columns in r. Probably the most critical one is correcting a mistake that was made when the data frame was originally created. For example, if you mistype “hamburger” as “hambuger,” you are going to want to correct it. You have the same situation if you accidentally use the wrong word. Being able to correct mistakes in data is always an important application of any operation in programming. You may even come up with a better column name after evaluating the data frame for a while. Regardless of the reasons, being able to change data frame column names is a useful tool.

When you rename multiple columns in r the names and rename functions are the tools to use. You can use them to rename single columns, multiple columns, or all the columns in a data frame. They each have their own advantages and disadvantages depending upon the situation. However, once you get the hang of them, they are easy to use.

Scroll to top
Privacy Policy