Renaming multiple variables to single variable name

Hi
I have 3 data frames for students of classes 1,2 and 3. The data is on an assessment survey which tests the students' academic performance. There are a few questions which are common across the three classes/grades. Let's assume a question- "Identification of animals". This question is common across the three grades. the question will be coded as "l1c1_identify_pic1" (for grade 1), "l2c1_identify_pic1" (for grade 2) and "l3c1_identify_pic1" (for grade 3). I want to give a common variable name "lc1". i have given sample data frame "grade1", "grade2" and "grade3". How can I do that?

library(tidyverse)

grade1<-tibble::tribble(
  ~student_id, ~l1c1_identify_pic1, ~l1c2_color, ~l1c3_shape, ~l1c4_memory,
       "S001",                  1L,          0L,          1L,           0L,
       "S002",                  1L,          0L,          0L,           0L,
       "S003",                  1L,          1L,          0L,           0L,
       "S004",                  1L,          0L,          0L,           1L,
       "S005",                  0L,          0L,          1L,           0L,
       "S006",                  0L,          1L,          0L,           0L,
       "S007",                  1L,          0L,          1L,           1L,
       "S008",                  1L,          0L,          0L,           0L,
       "S009",                  0L,          0L,          0L,           1L,
       "S010",                  0L,          0L,          0L,           1L
  )

garde2<-tibble::tribble(
  ~student_id, ~l2c1_identify_pic1, ~l2c2_color, ~l2c3_shape, ~l2c4_memory,
       "S011",                  1L,          0L,          1L,           0L,
       "S012",                  0L,          1L,          0L,           1L,
       "S013",                  1L,          1L,          1L,           0L,
       "S014",                  1L,          1L,          1L,           1L,
       "S015",                  0L,          0L,          1L,           0L,
       "S016",                  1L,          1L,          1L,           0L,
       "S017",                  0L,          0L,          1L,           0L,
       "S018",                  1L,          1L,          1L,           0L,
       "S019",                  1L,          1L,          0L,           1L,
       "S020",                  0L,          1L,          0L,           0L
  )

grade3<-tibble::tribble(
          ~student_id, ~l3c1_identify_pic1, ~l3c2_color, ~l3c3_shape, ~l3c4_memory,
               "S021",                  1L,          1L,          0L,           1L,
               "S022",                  1L,          0L,          0L,           1L,
               "S023",                  1L,          0L,          0L,           1L,
               "S024",                  0L,          1L,          0L,           0L,
               "S025",                  1L,          0L,          0L,           0L,
               "S026",                  1L,          0L,          1L,           1L,
               "S027",                  0L,          1L,          0L,           1L,
               "S028",                  1L,          0L,          0L,           1L,
               "S029",                  0L,          1L,          1L,           1L,
               "S030",                  0L,          1L,          1L,           0L
          )
names(grade1)[2] <- "lc1"

etc.

1 Like

As @nirgrahamuk shows, this is quite simple for a handful of objects to change. Sometimes, there can be an inconveniently large number. While this script won't reprex properly, it can be adopted to the cases where there might be not only grades but classrooms.

library(tibble)

p <- ls(pattern = "^grade")

grade1 <- tribble(
  ~student_id, ~l1c1_identify_pic1, ~l1c2_color, ~l1c3_shape, ~l1c4_memory,
  "S001", 1L, 0L, 1L, 0L,
  "S002", 1L, 0L, 0L, 0L,
  "S003", 1L, 1L, 0L, 0L,
  "S004", 1L, 0L, 0L, 1L,
  "S005", 0L, 0L, 1L, 0L,
  "S006", 0L, 1L, 0L, 0L,
  "S007", 1L, 0L, 1L, 1L,
  "S008", 1L, 0L, 0L, 0L,
  "S009", 0L, 0L, 0L, 1L,
  "S010", 0L, 0L, 0L, 1L
)

grade2 <- tribble(
  ~student_id, ~l2c1_identify_pic1, ~l2c2_color, ~l2c3_shape, ~l2c4_memory,
  "S011", 1L, 0L, 1L, 0L,
  "S012", 0L, 1L, 0L, 1L,
  "S013", 1L, 1L, 1L, 0L,
  "S014", 1L, 1L, 1L, 1L,
  "S015", 0L, 0L, 1L, 0L,
  "S016", 1L, 1L, 1L, 0L,
  "S017", 0L, 0L, 1L, 0L,
  "S018", 1L, 1L, 1L, 0L,
  "S019", 1L, 1L, 0L, 1L,
  "S020", 0L, 1L, 0L, 0L
)


grade3 <- tribble(
  ~student_id, ~l3c1_identify_pic1, ~l3c2_color, ~l3c3_shape, ~l3c4_memory,
  "S021", 1L, 1L, 0L, 1L,
  "S022", 1L, 0L, 0L, 1L,
  "S023", 1L, 0L, 0L, 1L,
  "S024", 0L, 1L, 0L, 0L,
  "S025", 1L, 0L, 0L, 0L,
  "S026", 1L, 0L, 1L, 1L,
  "S027", 0L, 1L, 0L, 1L,
  "S028", 1L, 0L, 0L, 1L,
  "S029", 0L, 1L, 1L, 1L,
  "S030", 0L, 1L, 1L, 0L
)

# check the variable names

before <- lapply(p, function(x) {
  if (is.data.frame(get(x))) {
    return(colnames(get(x)))
  } else {
    return(NULL)
  }
})

before

# n name for replacement variable name
n <- "lc1"
# list of names of identical data frames to change
l <- list(grade1, grade2, grade3)

new_name <- function(x) {
  colnames(l[[x]])[2] <- n
  return(l[[x]])
}

l <- lapply(seq_along(l), new_name)

for (i in seq_along(p)) assign(p[i], l[i][[1]])

# confirm changes

after <- lapply(p, function(x) {
  if (is.data.frame(get(x))) {
    return(colnames(get(x)))
  } else {
    return(NULL)
  }
})

after
1 Like

Thanks for this.. It works for me.

1 Like

This topic was automatically closed 7 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.