Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming the last column of a R dataFrame

Tags:

r

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?

like image 454
user3841581 Avatar asked Mar 14 '16 22:03

user3841581


3 Answers

The following should do what you need:

colnames(df)[ncol(df)] <- "space"
like image 173
Shu Avatar answered Nov 17 '22 21:11

Shu


Marginally less typing:

names(df)[ncol(df)] <- "space"
like image 25
Peter Avatar answered Nov 17 '22 19:11

Peter


You can use names() instead:

names(df)[length(names(df))]<-"space" 
like image 20
HubertL Avatar answered Nov 17 '22 19:11

HubertL