# How to change column names in data frame in R?

 names(mydat)[c(name)]<-c("newname")


From this, I know that the column/variable name "name" of the data frame mydat is replaced with "newname".

My question is if, I want to do this by a loop so that I will have some thing like:

newname1 newname2 newname3 newname4 and so on, how do I do it?

This is what did and it did not work:

for(i in 1:4){
names(mydat)[c(name)]<-c("newname"i)
}


Is there a way to code this? many thanks to all who could be of help. Owusu Isaac

-
This question appears to be off-topic because it is purely about programming. –  gung Sep 19 at 1:48

Most obvious solution would be to change your code in for loop with the following:

   names(mydat)[c(name)] <- paste("newname",i,sep="")


But you need to clarify what your variable name is. At the moment this loop will do 4 renames of the single column.

In general if the names which you want to change are in vector, this is a standard subsetting procedure:

 names(mydat)[names(mydat)%in% names_to_be_changed] <- name_changes

-
Hello Mpiktas, your solution works excellently. many thanks. and thanks to all as well –  Son May 10 '11 at 13:17
@Owusu, the expression of gratitude in this site is upvoting of the answers and their acceptance. See the faq. Either way, glad to be helpful. –  mpiktas May 10 '11 at 14:51

Try using sprintf or paste, like this:

names(mydat)<-sprintf("name%d",1:10)


Also, note that the names(mydat)[c(name)] is a more-less a nonsense; c(name) is equivalent to writing just name and means "get the value of variable called name'; bracket will at least extract elements of names(mydat) but only if name variable holds a numeric or boolean index.

If you want to replace columns called name with name1, name2, ..., nameN, use something like this:

names(mydat)[names(mydat)=="name"]<-sprintf("name%d",1:sum(names(mydat)=="name"))


EDIT: Well, if you just want to remove duplicated column names, there is even easier way; R has a make.names function which fixes this problem; it can be used like this:

names(mydat)<-make.names(names(mydat),unique=TRUE)


Even shorter, the same can be obtained only by writing:

data.frame(mydat)->mydat #The magic is in check.names, but it is TRUE by default

-
@mbq, == does partial matching for strings? –  mpiktas May 9 '11 at 16:50
@mpiktas No, but only exact matching is needed here. grepl would be OK to do the more general job. –  mbq May 9 '11 at 17:13
@mbq, yes but that assumes that there are multiple columns with the same name. R allows that, but I try to avoid using this feature. –  mpiktas May 10 '11 at 7:50
@mpiktas Apparently so does the OP, thus this question. –  mbq May 10 '11 at 8:57
@mbq thanks for clarifying the confusion with names(mydat)[c(name)] Since the OP has written I know that the column/variable name "name" I was like "wow that is possible ?" and tried to access the names by name ... stupid me ;). May I humbly suggest to edit the question ? –  steffen May 11 '11 at 7:49
show 1 more comment