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

 

                                                                                                 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts