5
set.seed(0)
data=data.frame("V1"=sample(1:10,size=4,rep=T),
                "V2"=sample(1:10,size=4,rep=T),
                "V3"=sample(1:10,size=4,rep=T),
                "V4"=sample(1:10,size=4,rep=T))

names = data.frame("vars"=c("V1", "V2", "V3", "V4"),
                   "labels"=c("whale","toast","cheese","cow"))


want=data.frame("whale"=sample(1:10,size=4,rep=T),
                "toast"=sample(1:10,size=4,rep=T),
                "cheese"=sample(1:10,size=4,rep=T),
                "cow"=sample(1:10,size=4,rep=T))

I have data, "data" and a dataframe "names" that contains the col names of "data" and the variable labels.

I want to create new data "want" which replaces the variable names in "data" with the variable labels in "names" Note however the actual values are different because I do not know how to ensure the same sampled values!

tjebo
  • 21,977
  • 7
  • 58
  • 94
bvowe
  • 3,004
  • 3
  • 16
  • 33

4 Answers4

8

We can use rename_at from dplyr (assuming the columns in 'names' dataset are character class)

library(dplyr)
data <- data %>% 
            rename_at(vars(names$vars), ~ names$labels)
data
#   whale toast cheese cow
#1     9     2      1   6
#2     4     7      5  10
#3     7     2      5   7
#4     1     3     10   9

Or convert to a named vector by deframeing and then directly match

names %>% 
     mutate_all(as.character) %>%
     deframe %>%
     {set_names(data, .[names(data)])}
akrun
  • 874,273
  • 37
  • 540
  • 662
6

Another option using a named vector, but with rename and !!!:

library(dplyr)

# make your named vector (maybe easier to do this directly instead of creating 
#   the data frame first), e.g. 
# name_vec <- setNames(c("V1", "V2", "V3", "V4"), 
#                      c("whale", "toast", "cheese", "cow"))

name_vec <- setNames(as.character(names$vars),as.character( names$labels))

data %>% rename(!!!name_vec)

#>   whale toast cheese cow
#> 1     9     2      1   6
#> 2     4     7      5  10
#> 3     7     2      5   7
#> 4     1     3     10   9

Created on 2020-02-09 by the reprex package (v0.3.0)

Azor Ahai -him-
  • 123
  • 1
  • 7
tjebo
  • 21,977
  • 7
  • 58
  • 94
3

Are you asking for this? Using your example:

> names(data) <- names$labels
> names(data)
[1] "whale"  "toast"  "cheese" "cow"   
novica
  • 655
  • 4
  • 11
1

You can get the same data frame by setting the same seed:

set.seed(0)
data=data.frame("V1"=sample(1:10,size=4,rep=T),
                "V2"=sample(1:10,size=4,rep=T),
                "V3"=sample(1:10,size=4,rep=T),
                "V4"=sample(1:10,size=4,rep=T))

names = data.frame("vars"=c("V1", "V2", "V3", "V4"),
                   "labels"=c("whale","toast","cheese","cow"))

set.seed(0)
want=data.frame("whale"=sample(1:10,size=4,rep=T),
                "toast"=sample(1:10,size=4,rep=T),
                "cheese"=sample(1:10,size=4,rep=T),
                "cow"=sample(1:10,size=4,rep=T))

And to get the names, better to use match:

want = setNames(
data.frame(data),
names$labels[match(colnames(data),names$vars)]
)
StupidWolf
  • 45,075
  • 17
  • 40
  • 72