Rename Columns in a DataFrame in R

Here are several ways to rename columns in a DataFrame in R:

(1) Use the colnames() function to rename column/s in a DataFrame in R:

By specifying the column name to rename a single column:

colnames(df)[colnames(df) == "old_column_name"] <- "new_column_name"

By specifying the column index to rename a single column:

colnames(df)[column_index] <- "new_column_name"

By specifying the column indexes to rename multiple columns:

colnames(df)[c(column_index_1, column_index_2, ...)] <- c("new_column_name_1", "new_column_name_2", ...)

(2) Use the names() function to rename column/s:

By specifying the column name to rename a single column:

names(df)[names(df) == "old_column_name"] <- "new_column_name"

By specifying the column index to rename a single column:

names(df)[column_index] <- "new_column_name"

By specifying the column indexes to rename multiple columns:

names(df)[c(column_index_1, column_index_2, ...)] <- c("new_column_name_1", "new_column_name_2", ...)

Examples

Example 1: Rename a single column using the colnames() function

First, create a DataFrame in R with 3 columns: product, brand and price:

df <- data.frame(product = c("laptop", "printer", "tablet", "desk", "chair"),
                 brand = c("A", "B", "X", "Y", "Z"),
                 price = c(1200, 150, 300, 450, 200)
                 )

print(df)

Here is the DataFrame:

  product brand price
1  laptop     A  1200
2 printer     B   150
3  tablet     X   300
4    desk     Y   450
5   chair     Z   200

Use the colnames() function to rename the second column from “brand” to “category” as follows:

df <- data.frame(product = c("laptop", "printer", "tablet", "desk", "chair"),
                 brand = c("A", "B", "X", "Y", "Z"),
                 price = c(1200, 150, 300, 450, 200)
                 )

colnames(df)[colnames(df) == "brand"] <- "category"

print(df)

The new column name is now “category” as highlighted in yellow:

  product category price
1  laptop        A  1200
2 printer        B   150
3  tablet        X   300
4    desk        Y   450
5   chair        Z   200

Alternatively, you can use the column index of 2 to rename the “brand” column to “category” as follows:

df <- data.frame(product = c("laptop", "printer", "tablet", "desk", "chair"),
                 brand = c("A", "B", "X", "Y", "Z"),
                 price = c(1200, 150, 300, 450, 200)
                 )

colnames(df)[2] <- "category"

print(df)

You’ll get the same results:

  product category price
1  laptop        A  1200
2 printer        B   150
3  tablet        X   300
4    desk        Y   450
5   chair        Z   200

Example 2: Rename multiple columns using the colnames() function

Use the colnames() function to rename the first and third columns (the “product” column would be renamed to “device,” while the “price” column would be renamed to “cost”):

df <- data.frame(product = c("laptop", "printer", "tablet", "desk", "chair"),
                 brand = c("A", "B", "X", "Y", "Z"),
                 price = c(1200, 150, 300, 450, 200)
                 )

colnames(df)[c(1, 3)] <- c("device", "cost")

print(df)

Here are the renamed columns:

   device brand cost
1  laptop     A 1200
2 printer     B  150
3  tablet     X  300
4    desk     Y  450
5   chair     Z  200

Example 3: Rename a single column using the names() function

Use the names() function to rename the second column from “brand” to “category” as captured below:

df <- data.frame(product = c("laptop", "printer", "tablet", "desk", "chair"),
                 brand = c("A", "B", "X", "Y", "Z"),
                 price = c(1200, 150, 300, 450, 200)
                 )

names(df)[names(df) == "brand"] <- "category"

print(df)

The new column name is now “category”:

  product category price
1  laptop        A  1200
2 printer        B   150
3  tablet        X   300
4    desk        Y   450
5   chair        Z   200

Or by the column index:

df <- data.frame(product = c("laptop", "printer", "tablet", "desk", "chair"),
                 brand = c("A", "B", "X", "Y", "Z"),
                 price = c(1200, 150, 300, 450, 200)
                 )

names(df)[2] <- "category"

print(df)

As before, the new column name is now “category”:

  product category price
1  laptop        A  1200
2 printer        B   150
3  tablet        X   300
4    desk        Y   450
5   chair        Z   200

Example 4: Rename multiple columns using the names() function

Use the names() function to rename the first and third columns:

df <- data.frame(product = c("laptop", "printer", "tablet", "desk", "chair"),
                 brand = c("A", "B", "X", "Y", "Z"),
                 price = c(1200, 150, 300, 450, 200)
                 )

names(df)[c(1, 3)] <- c("device", "cost")

print(df)

The result:

   device brand cost
1  laptop     A 1200
2 printer     B  150
3  tablet     X  300
4    desk     Y  450
5   chair     Z  200

Leave a Comment