Renaming the last column of a R dataFrame

Viewed 9305

I have a R dataFrame df with the following:

time      value      reference
 45        10           11
 22        12           10
 13        15           5

I would like to replace the last column of the dataFrame to obtain:

time      value        space
 45        10           11
 22        12           10
 13        15           5

I tried this:

colnames(length(colnames(df)))<-"space" 

but it does not work. How can I do it?

3 Answers

Marginally less typing:

names(df)[ncol(df)] <- "space"

The following should do what you need:

colnames(df)[ncol(df)] <- "space"
Related