Find and Replace variables from a given list

Hi,
I have a bag dataset with around 600 variables. I need to replace the variables with the new names given in a list. The example has been posted below. Here the variables in the dataframe "data" have to be changed as given the column 'new variable' in the 'list'. How can I achieve this?

library(tidyverse)
library(janitor)

tibble::tribble(
  list<- ~old_variable,  ~new_variable,
  "l2c1_id_pic1", "l1c1_id_pic1",
  "l2c1_id_pic2", "l1c1_id_pic2",
  "l2c2_id_col1", "l1c2_id_col1",
  "l2c2_id_col2", "l1c2_id_col2",
    "l3el1_desc",    "l1c1_desc"
  )


data<-tibble::tribble(
        ~student_id, ~l1c1_id_pic1, ~l1c1_id_pic2, ~l2c1_id_col1, ~l2c1_id_col2, ~l3el1_desc,
              "XXC",            1L,            1L,            1L,            1L,          1L,
              "GGH",            1L,            1L,            0L,            0L,          1L,
              "SHS",            1L,            0L,            1L,            0L,          1L,
              "DDK",            1L,            1L,            0L,            0L,          1L,
              "DJS",            0L,            1L,            0L,            0L,          1L
        )

I think you are looking for the rename_with() function here?

var_list <- tibble::tribble(
  list <- ~old_variable, ~new_variable,
  "l2c1_id_pic1", "l1c1_id_pic1",
  "l2c1_id_pic2", "l1c1_id_pic2",
  "l2c2_id_col1", "l1c2_id_col1",
  "l2c2_id_col2", "l1c2_id_col2",
  "l3el1_desc", "l1c1_desc"
)

d <- tibble::tribble(
  ~student_id, ~l1c1_id_pic1, ~l1c1_id_pic2, ~l2c1_id_col1, ~l2c1_id_col2, ~l3el1_desc,
  "XXC", 1L, 1L, 1L, 1L, 1L,
  "GGH", 1L, 1L, 0L, 0L, 1L,
  "SHS", 1L, 0L, 1L, 0L, 1L,
  "DDK", 1L, 1L, 0L, 0L, 1L,
  "DJS", 0L, 1L, 0L, 0L, 1L
)

colnames(d) <- c("student_id",unlist(var_list[2]))
str(d)
#> tibble [5 × 6] (S3: tbl_df/tbl/data.frame)
#>  $ student_id  : chr [1:5] "XXC" "GGH" "SHS" "DDK" ...
#>  $ l1c1_id_pic1: int [1:5] 1 1 1 1 0
#>  $ l1c1_id_pic2: int [1:5] 1 1 0 1 1
#>  $ l1c2_id_col1: int [1:5] 1 0 1 0 0
#>  $ l1c2_id_col2: int [1:5] 1 0 0 0 0
#>  $ l1c1_desc   : int [1:5] 1 1 1 1 1

Created on 2023-05-02 by the reprex package (v2.0.1)

Thanks for the response. I actually want it in another format. You have changed into horizontal format. It has to be as given in the data frame 'data1'.

library(tidyverse)
library(janitor)

tibble::tribble(
   ~old_variable,  ~new_variable,
  "l2c1_id_pic1", "l1c1_id_pic1",
  "l2c1_id_pic2", "l1c1_id_pic2",
  "l2c2_id_col1", "l1c2_id_col1",
  "l2c2_id_col2", "l1c2_id_col2",
    "l3el1_desc",    "l1c1_desc"
  )


data<-tibble::tribble(
        ~student_id, ~l1c1_id_pic1, ~l1c1_id_pic2, ~l2c1_id_col1, ~l2c1_id_col2, ~l3el1_desc,
              "XXC",            1L,            1L,            1L,            1L,          1L,
              "GGH",            1L,            1L,            0L,            0L,          1L,
              "SHS",            1L,            0L,            1L,            0L,          1L,
              "DDK",            1L,            1L,            0L,            0L,          1L,
              "DJS",            0L,            1L,            0L,            0L,          1L
        )

data1<-tibble::tribble(
         ~student_id, ~l1c1_id_pic1, ~l1c1_id_pic2, ~l1c1_id_col1, ~l1c1_id_col2, ~l1el1desc,
               "XXC",            1L,            1L,            1L,            1L,         1L,
               "GGH",            1L,            1L,            0L,            0L,         1L,
               "SHS",            1L,            0L,            1L,            0L,         1L,
               "DDK",            1L,            1L,            0L,            0L,         1L,
               "DJS",            0L,            1L,            0L,            0L,         1L
         )

str(d) isn't for display; it describes the structure. Calling d directly gives

> d
# A tibble: 5 × 6
  student_id l1c1_id_pic1 l1c1_id_pic2 l1c2_id_col1 l1c2_id…¹ l1c1_…²
  <chr>             <int>        <int>        <int>     <int>   <int>
1 XXC                   1            1            1         1       1
2 GGH                   1            1            0         0       1
3 SHS                   1            0            1         0       1
4 DDK                   1            1            0         0       1
5 DJS                   0            1            0         0       1
# … with abbreviated variable names ¹​l1c2_id_col2, ²​l1c1_desc
>
library(tidyverse)
# library(janitor)

nm_lkup <- tibble::tribble(
  ~old_variable, ~new_variable,
  "l2c1_id_pic1", "l1c1_id_pic1",
  "l2c1_id_pic2", "l1c1_id_pic2",
  "l2c2_id_col1", "l1c2_id_col1",
  "l2c2_id_col2", "l1c2_id_col2",
  "l3el1_desc", "l1c1_desc"
)


data <- tibble::tribble(
  ~student_id, ~l1c1_id_pic1, ~l1c1_id_pic2, ~l2c1_id_col1, ~l2c1_id_col2, ~l3el1_desc,
  "XXC", 1L, 1L, 1L, 1L, 1L,
  "GGH", 1L, 1L, 0L, 0L, 1L,
  "SHS", 1L, 0L, 1L, 0L, 1L,
  "DDK", 1L, 1L, 0L, 0L, 1L,
  "DJS", 0L, 1L, 0L, 0L, 1L
)

data2 <- rename_with(data,
  .cols = intersect(
    nm_lkup$old_variable,
    names(data)
  ),
  .fn = \(x){
    nm_lkup$new_variable[
      which(x == nm_lkup$old_variable)
    ]
  }
)

# this changes l3el1_desc -> l1c1_desc as per the requirements given

The recodeflow package may help. You set up the metadata of your variables in a table (or CSV) file with your variable names and labels.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.