Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

15. Getting Your Data into Shape > Renaming Columns in a Data Frame

Renaming Columns in a Data Frame

Problem

You want to rename the columns in a data frame.

Solution

Use the names(dat) <- function:

names(dat) <- c("name1", "name2", "name3")

Discussion

If you want to rename the columns by name:

library(gcookbook) # For the data set
names(anthoming)   # Print the names of the columns

 "angle" "expt"  "ctrl"

names(anthoming)[names(anthoming) == "ctrl"] <- c("Control")
names(anthoming)[names(anthoming) == "expt"] <- c("Experimental")
names(anthoming)

 "angle"        "Experimental" "Control"

They can also be renamed by numeric position:

names(anthoming)[1] <- "Angle"
names(anthoming)

 "Angle"        "Experimental" "Control"

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint