Rename the column in R

To rename the column in R we use different methods like renaming all the columns in R and rename the specific column in R. In this tutorial we will be looking on how to

  • Rename all the columns in R
  • Rename only specific column

Let’s first create the dataframe.

df1=data.frame(State=c('Arizona','Georgia', 'Newyork','Indiana','seattle','washington','Texas'),
               code=c('AZ','GA','NY','IN','ST','WT','TX'),
               Score=c(62,47,55,74,31,77,85))
df1

So the dataframe will be

rename column in R 1

 

Rename all the column in R:

# rename all the column in R

colnames(df1) <- c("State_Name", "State_code","Hindex_Score")
df1

so the resultant dataframe will be

rename column in R 2

 

Rename a specific column in R – Method 1:

Rename the first column as “US_State_Name”

# rename a specific column in R

names(df1)[1]<-"US_State_Name"
df1

so the resultant dataframe will be

rename column in R 3

 

Rename a specific column in R – Method 2:

Rename the “State” column as “US_State_Name”

# rename a specific column in R

names(df1)[names(df1) == "State"] <- "US_State_Name" 
df1

so the resultant dataframe will be

rename column in R 4

 

Rename the column in R - image previous-small-11-1 on http://www.datasciencemadesimple.com                                                                                                        Rename the column in R - image next_small-11-1 on http://www.datasciencemadesimple.com