Changing column names of a data frame

RDataframeRename

R Problem Overview


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:

names(newprice)[1]<-paste("premium")
names(newprice)[2]<-paste("change")
names(newprice)[3]<-paste("newprice") 

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?

R Solutions


Solution 1 - R

Use the colnames() function:

R> X <- data.frame(bad=1:3, worse=rnorm(3))
R> X
  bad     worse
1   1 -2.440467
2   2  1.320113
3   3 -0.306639
R> colnames(X) <- c("good", "better")
R> X
  good    better
1    1 -2.440467
2    2  1.320113
3    3 -0.306639

You can also subset:

R> colnames(X)[2] <- "superduper"
 

Solution 2 - R

I use this:

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

Solution 3 - R

The error is caused by the "smart-quotes" (or whatever they're called). The lesson here is, "don't write your code in an 'editor' that converts quotes to smart-quotes".

names(newprice)[1]<-paste(“premium”)  # error
names(newprice)[1]<-paste("premium")  # works

Also, you don't need paste("premium") (the call to paste is redundant) and it's a good idea to put spaces around <- to avoid confusion (e.g. x <- -10; if(x<-3) "hi" else "bye"; x).

Solution 4 - R

Try:

names(newprice)[1] <- "premium"

Solution 5 - R

The new recommended way to do this is to use the setNames function. See ?setNames. Since this creates a new copy of the data.frame, be sure to assign the result to the original data.frame, if that is your intention.

data_frame <- setNames(data_frame, c("premium","change","newprice"))

Newer versions of R will give you warning if you use colnames in some of the ways suggested by earlier answers.

If this were a data.table instead, you could use the data.table function setnames, which can modify specific column names or a single column name by reference:

setnames(data_table, "old-name", "new-name")

Solution 6 - R

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.

Solution 7 - R

Similar to the others:

cols <- c("premium","change","newprice")
colnames(dataframe) <- cols

Quite simple and easy to modify.

Solution 8 - R

Use this to change column name by colname function.

colnames(newprice)[1] = "premium"
colnames(newprice)[2] = "change"
colnames(newprice)[3] = "newprice"

Solution 9 - R

If you need to rename not all but multiple column at once when you only know the old column names you can use colnames function and %in% operator. Example:

df = data.frame(bad=1:3, worse=rnorm(3), worst=LETTERS[1:3])

   bad      worse    worst
1   1 -0.77915455       A
2   2  0.06717385       B
3   3 -0.02827242       C

Now you want to change "bad" and "worst" to "good" and "best". You can use

colnames(df)[which(colnames(df) %in% c("bad","worst") )] <- c("good","best")

This results in

  good      worse  best
1    1 -0.6010363    A
2    2  0.7336155    B
3    3  0.9435469    C

Solution 10 - R

There are a couple options with dplyr::rename() and dplyr::select():

library(dplyr)

mtcars %>% 
  tibble::rownames_to_column('car_model') %>%                            # convert rowname to a column. tibble must be installed.
  select(car_model, est_mpg = mpg, horse_power = hp, everything()) %>%   # rename specific columns and reorder
  rename(weight = wt, cylinders = cyl) %>%                               # another option for renaming specific columns that keeps everything by default
  head(2)
      car_model est_mpg horse_power cylinders disp drat weight  qsec vs am gear carb
1     Mazda RX4      21         110         6  160  3.9  2.620 16.46  0  1    4    4
2 Mazda RX4 Wag      21         110         6  160  3.9  2.875 17.02  0  1    4    4

There are also three scoped variants of dplyr::rename(): dplyr::rename_all() for all column names, dplyr::rename_if() for conditionally targeting column names, and dplyr::rename_at() for select named columns. The following example replaces spaces and periods with an underscore and converts everything to lower case:

iris %>%  
  rename_all(~gsub("\\s+|\\.", "_", .)) %>% 
  rename_all(tolower) %>% 
  head(2)
  sepal_length sepal_width petal_length petal_width species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa

dplyr::select_all() can also be used in a similar way:

iris %>%  
  select_all(~gsub("\\s+|\\.", "_", .)) %>% 
  select_all(tolower) %>% 
  head(2)
  sepal_length sepal_width petal_length petal_width species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa

Solution 11 - R

try:

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

Solution 12 - R

My column names is as below

colnames(t)
[1] "Class"    "Sex"      "Age"      "Survived" "Freq" 

I want to change column name of Class and Sex

colnames(t)=c("STD","Gender","AGE","SURVIVED","FREQ")

Solution 13 - R

Just to correct and slightly extend Scott Wilson answer.
You can use data.table's setnames function on data.frames too.

Do not expect speed up of the operation but you can expect the setnames to be more efficient for memory consumption as it updates column names by reference. This can be tracked with address function, see below.

library(data.table)
set.seed(123)
n = 1e8

df = data.frame(bad=sample(1:3, n, TRUE), worse=rnorm(n))
address(df)
#[1] "0x208f9f00"
colnames(df) <- c("good", "better")
address(df)
#[1] "0x208fa1d8"
rm(df)

dt = data.table(bad=sample(1:3, n, TRUE), worse=rnorm(n))
address(dt)
#[1] "0x535c830"
setnames(dt, c("good", "better"))
address(dt)
#[1] "0x535c830"
rm(dt)

So if you are hitting your memory limits you may consider to use this one instead.

Solution 14 - R

You can just do the editing by:

newprice <- edit(newprice)

and change the column name manually.

Solution 15 - R

This may be helpful:

rename.columns=function(df,changelist){
  #renames columns of a dataframe
  for(i in 1:length(names(df))){
    if(length(changelist[[names(df)[i]]])>0){
      names(df)[i]= changelist[[names(df)[i]]]
    }
  }
  df
}

# Specify new dataframe
df=rename.columns(df,list(old.column='new.column.name'))

Solution 16 - R

In case we have 2 dataframes the following works

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

We change names of DF1 as follows

 colnames(DF1)<- colnames(DF2)

Solution 17 - R

One option using data.table:

library(data.table)

setDT(dataframe)

setnames(dataframe,'Old1','New1')
setnames(dataframe,'Old2','New2')

Solution 18 - R

Change data frame column name

colnames(dataset)[colnames(dataset) == 'name'] <- 'newcolumnname'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSonView Question on Stackoverflow
Solution 1 - RDirk EddelbuettelView Answer on Stackoverflow
Solution 2 - RMatheus AbreuView Answer on Stackoverflow
Solution 3 - RJoshua UlrichView Answer on Stackoverflow
Solution 4 - RJamieView Answer on Stackoverflow
Solution 5 - RScott C WilsonView Answer on Stackoverflow
Solution 6 - RDesta Haileselassie HagosView Answer on Stackoverflow
Solution 7 - RAdam EricksonView Answer on Stackoverflow
Solution 8 - RSophannaView Answer on Stackoverflow
Solution 9 - RdiscipulusView Answer on Stackoverflow
Solution 10 - RsbhaView Answer on Stackoverflow
Solution 11 - RngamitaView Answer on Stackoverflow
Solution 12 - RMehul KataraView Answer on Stackoverflow
Solution 13 - RjangoreckiView Answer on Stackoverflow
Solution 14 - RBaykalView Answer on Stackoverflow
Solution 15 - RChrisView Answer on Stackoverflow
Solution 16 - RRaghavan vmvsView Answer on Stackoverflow
Solution 17 - RkakarotView Answer on Stackoverflow
Solution 18 - RRupesh KumarView Answer on Stackoverflow