dataframe change one - Changing column names of a data frame

8 Answers

I use this:

colnames(dataframe)[which(names(dataframe) == "columnName")] <- "newColumnName"
rename dplyr multiple

I have a data frame called "newprice" (see below) and I want to change the column names in my program in R.

> newprice
   Chang.  Chang.   Chang.
1     100       36      136
2     120      -33       87
3     150       14      164

In fact this is what am doing:


I have not put this in a loop because I want each column name to be different as you see.

When I paste my program into R console this is the output it gives me:

> names(newprice)[1]<-paste(“premium”)
Error: unexpected input in "names(newprice)[1]<-paste(“"
> names(newprice)[2]<-paste(“change”)
Error: unexpected input in "names(newprice)[2]<-paste(“"
> names(newprice)[3]<-paste(“newpremium”)
Error: unexpected input in "names(newprice)[3]<-paste(“"

I have equally tried using the c() function-for example c("premium"), instead of the paste() function, but to no avail.

Could someone help me to figure this out?

Did you try just:



I had the same issue and this piece of code worked out for me.

names(data)[names(data) == "oldVariableName"] <- "newVariableName"

In short, this code does the following:

names(data) looks into all the names in the dataframe (data)

[names(data) == oldVariableName] extracts the variable name (oldVariableName) you want to get renamed and <- "newVariableName" assigns the new variable name.

You can just do the editing by:

newprice <- edit(newprice)

and change the column name manually.


names(newprice) <- c("premium", "change", "newprice")

You could straightaway have done

names(newprice) <- c("premium","change","newprice")

The paste command that you are using takes 2 arguments atleast. It works like concatenate function in excel which is why it is giving you an error i think.

My column names is as below

[1] "Class"    "Sex"      "Age"      "Survived" "Freq" 

I want to change column name of Class and Sex


In case we have 2 dataframes the following works

 DF1<-data.frame('a', 'b')

We change names of DF1 as follows

 colnames(DF1)<- colnames(DF2)



r   dataframe