Rename Columns Based On Vector [duplicate]
Asked Answered
S

4

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!

Simian answered 8/2, 2020 at 22:54 Comment(0)
D
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)])}
Drudgery answered 8/2, 2020 at 23:22 Comment(2)
where is deframe from?Misdeed
@Tjebo sorry, it is from library(tibble)Drudgery
M
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)

Misdeed answered 9/2, 2020 at 12:40 Comment(0)
C
3

Are you asking for this? Using your example:

> names(data) <- names$labels
> names(data)
[1] "whale"  "toast"  "cheese" "cow"   
Calbert answered 8/2, 2020 at 22:58 Comment(0)
K
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)]
)
Knob answered 8/2, 2020 at 23:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.