Rename Columns in R

How to Rename Columns in R

This page will show you how to rename columns in R with examples using either the existing column name or the column number to specify which column name to change.

Warning: R will allow a field to be named with a space but you won’t be able to easily refer to that column after the name change. If you need to make column names more readable for presentation do this as a final step just before exporting the data from R.

The first example shows how to change the name of a column using the name to identify which column apply the name to. The following code will rename the field SCHOOL_YEAR to School Year in the data frame called SCHOOL_DATA.

# Rename a column in R
colnames(SCHOOL_DATA)[colnames(SCHOOL_DATA)=="SCHOOL_YEAR"] <- "School Year"

The second example shows how to change the name of a column using the column number to identify which column apply the name to. The following code will rename third column in the data frame SCHOOL_DATA to the name WSAS_Reading.

# Rename a column in R
names(SCHOOL_DATA)[3]<-"WSAS Reading"

See official R-manual page on colnames to learn more: http://stat.ethz.ch/R-manual/R-patched/library/base/html/colnames.html.

Thanks for reading! This website took a great deal of time to create. If it was helpful to you, please show it by sharing with friends, liking, or tweeting! If you have any thoughts regarding this R code please post in the comments.

JM

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>