Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace data.frame column names with string in corresponding lookup table in R

Tags:

r

I have the following data.frame:

set.seed(126)
df <- data.frame(a=sample(c(1:100, NA), 10), b=sample(1:100, 10), c=sample(1:100, 10), d = c(1:10))
    a  b  c  d
1  18 27 53  1
2  44 16 66  2
3  58 47  3  3
...

And the following lookup table:

varnames <- data.frame(old = c("a", "b", "c"), new = c("dog", "cat", "mouse"))
  old   new
1   a   dog
2   b   cat
3   c mouse

What I am trying to do is replace the names(df) with the corresponding varnames$new... If a names(df) is not in varnames$old, then retain the colname in df...

The resulting data.frame I would like returned would look like this:

   dog cat mouse  d
1   57  10    83  1
2   53  99    94  2
3   99  60    39  3
...

Any and all help appreciated.

like image 395
gh0strider18 Avatar asked Jun 04 '15 21:06

gh0strider18


People also ask

How to rename a column in R data frame?

colnames () method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame.

How to replace a value in a data frame with another?

You can use the following syntax to replace one of several values in a data frame with a new value: df [df == 'Old Value 1' | df == 'Old Value 2'] <- 'New value' And you can use the following syntax to replace a particular value in a specific column of a data frame with a new value: df ['column1'] [df ['column1'] == 'Old Value'] <- 'New value'

How to replace the values in a list in R?

Method 1: Using Replace () function. replace () function in R Language is used to replace the values in the specified string vector x with indices given in list by those given in values. It takes on three parameters first is the list name, then the index at which the element needs to be replaced, and the third parameter is the replacement values.

How do I replace a column name with a different one?

Column names can also be replaced by using the which (names (df)) function, which searches for the column with the specified old name and then replaces it with the new specified name instance.


2 Answers

How about using the match() function

mm <- match(names(df), varnames$old)
names(df)[!is.na(mm)] <- as.character(varnames$new[na.omit(mm)])
head(df)
#   dog cat mouse d
# 1  65  48    19 1
# 2  46  15    80 2
# 3  NA  47    84 3
# 4  68  34    46 4
# 5  23  75    42 5
# 6  92  87    68 6

If you are interested, you could also use the dplyr rename() function

library(dplyr)
df %>% rename_(.dots=with(varnames, setNames(as.list(as.character(old)), new)))

Or one more option, the data.table package has a setnames function

library(data.table)
setnames(df, as.character(varnames$old), as.character(varnames$new))
like image 79
MrFlick Avatar answered Oct 05 '22 18:10

MrFlick


One more option is mapvalues() from the plyr package:

library(plyr)
names(df) <- mapvalues(names(df),
                             from = varnames$old,
                             to = as.character(varnames$new))

If you are using the dplyr package, you can call this with plyr::mapvalues() so you don't have to load plyr on top of dplyr (which causes problems).

like image 20
Sam Firke Avatar answered Oct 05 '22 17:10

Sam Firke