6

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()
9
  • 1
    Use colnames function. Commented Aug 12, 2016 at 15:24
  • You probably want the match function. Commented Aug 12, 2016 at 15:25
  • @nrussell Thanks, how do I go able using match? please forgive my ignorance :( Commented Aug 12, 2016 at 15:27
  • You need to include the output of dput(industries) and dput(trans) in your question. Commented Aug 12, 2016 at 15:30
  • 2
    @Warner, substituting by index seems error-prone. Safer to match by name. Commented Aug 12, 2016 at 15:52

3 Answers 3

8

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
Sign up to request clarification or add additional context in comments.

7 Comments

Why a downvote? This is essentially what I do when reading data with silly col names.
Just curious, why the downvote? If you think I've done something incorrect or sub-optimal, please let me know, so I can improve my code.
@eipi10 Thanks for the code above. I'm struggling to see how to make it fit into my little function above. Could someone help point out how it could be improved? Thanks
I don't think you need a new function to do it. Just do names(industries)[match(trans[ ,"old"], names(industries)] = trans[ ,"new"].
@eipi10 - thanks for the code above. The match function did it for me as you suggested. I can't tell you how thrilled it feels to see it working. I know... I know...i'm quite excited. I'm loving R programming.
|
3

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.