Helen Neely
Helen Neely

Reputation: 4740

Rename Columns with names from another data frame

I'm learning R programming as such have hit a few problems - and with your help have been able to fix them.

But I now have a need to rename columns of a data frame. I have a translation data frame with 2 columns that contains the column names and what the new columns should be called.

Here is my code: my question is how do I select the two columns from the trans dataframe and use them here as trans$old and trans$new variables?

I have 7 columns I'm renaming, and this might be even longer hence the translation table.

replace_header <- function()
{      
  names(industries)[names(industries)==trans$old] <- trans$new
  replaced <- industries
  return (replaced)
}  

replaced_industries <- replace_header()

Upvotes: 6

Views: 8839

Answers (3)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193687

I'd recommend setnames from "data.table" for this. Using @eipi10's example:

mt = mtcars
dat = data.frame(old=c("mpg","am"), new=c("new.name1","new.name2"), stringsAsFactors=FALSE)

library(data.table)
setnames(mt, dat$old, dat$new)
names(mt)
#  [1] "new.name1" "cyl"       "disp"      "hp"        "drat"      "wt"       
#  [7] "qsec"      "vs"        "new.name2" "gear"      "carb" 

If there's a concern as indicated by @jmbadia that the data.frame with the old and new names, you can add skip_absent=TRUE to setnames.

Upvotes: 3

jmbadia
jmbadia

Reputation: 1

improving a bit the eipi10's answer, if we want to use a "rename dataframe" with old names not always present on the mt dataframe (e.g. because mt is provided by differnt sources so we don't always know its colnames) we can consider the following code

mt = mtcars

head(mt,3)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1   4     4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1   4     4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1   4     1
# dataframe with possible names to replace
dat = data.frame(old=c("strangeName","am"), new=c("new.name1","new.name2"), stringsAsFactors=FALSE)
# find which old names are present in mt
namesMatched <- dat[dat$old %in% names(mt)
#renaming
names(mt)[match(namesMatched,"old"], names(mt))] = dat[namesMatched,"new"]

head(mt,3)
                   mpg cyl disp  hp drat    wt  qsec vs new.name2 gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0     1       4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0     1       4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1     1       4    1

Upvotes: 0

eipi10
eipi10

Reputation: 93871

Here's an example using the built-in mtcars data frame. We'll use the match function to find the indices of the columns names we want to replace and then replace them with new names.

# Copy of built-in data frame
mt = mtcars

head(mt,3)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
# Data frame with column name substitutions
dat = data.frame(old=c("mpg","am"), new=c("new.name1","new.name2"), stringsAsFactors=FALSE)

dat
  old       new
1 mpg new.name1
2  am new.name2

Use match to find the indices of the "old" names in the mt data frame:

match(dat[,"old"], names(mt))
[1] 1 9

Substitute "old" names with "new" names:

names(mt)[match(dat[,"old"], names(mt))] = dat[,"new"]

head(mt,3)
                  new.name1 cyl disp  hp drat    wt  qsec vs new.name2 gear carb
Mazda RX4              21.0   6  160 110 3.90 2.620 16.46  0         1    4    4
Mazda RX4 Wag          21.0   6  160 110 3.90 2.875 17.02  0         1    4    4
Datsun 710             22.8   4  108  93 3.85 2.320 18.61  1         1    4    1

Upvotes: 8

Related Questions