How to rename collected data; Possible? Need help

Hello,

I am currently evaluating a study as part of a seminar.

The scale created in the survey has the following variables:
4 = very negative
5 = negative
6 = rather negative
3 = rather positive
2 = positive
1 = very positive

Now I have the problem that it would have been better to have the variables in a different order:
1 = very negative
2 = negative
3 = rather negative
4 = rather positive
5 = positive
6 = very positive

I have to turn in my term paper on Sunday and I am really getting desperate right now and hope someone can help me. Is there a command I can use in R-Studio to change the names of the variables without losing the data? In particular, my created scales do not work with the old order of the scales ...

The rename should be:
4 -> 1
5 -> 2
6 -> 3
3 -> 4
2 -> 5
1 -> 6

Thanks in advance
Lea

Yes, the case_when() function is perfect for creating a new column of values while preserving the original. See code below.

data = tibble(old_scale = c(4, 5, 6, 3, 2, 1)) %>%
  mutate(new_scale = case_when(
    old_scale == 4 ~ 1,
    old_scale == 5 ~ 2,
    old_scale == 6 ~ 3,
    old_scale == 3 ~ 4,
    old_scale == 2 ~ 5,
    old_scale == 1 ~ 6,
  ))

data
#> # A tibble: 6 × 2
#>   old_scale new_scale
#>       <dbl>     <dbl>
#> 1         4         1
#> 2         5         2
#> 3         6         3
#> 4         3         4
#> 5         2         5
#> 6         1         6

Created on 2022-09-09 with reprex v2.0.2.9000

2 Likes

Thank you so much for your answer!

Can I ask you another question?

My dataset is called data_darkmode3. The scale is in 19 different columns. The participants had to rate 19 images in a 6 Likert scale.
Do I need to rename for each column?

Or is it possible like this?
data_darkmode3 = tibble(T001:T019 = c(4,5,6,3,2,1)) %>%
mutate(T001:T019 = case_when(
T001:T019 == 4 ~ 1,
T001:T019 == 5 ~ 2,
T001:T019 == 6 ~ 3,
T001:T019 == 3 ~ 4,
T001:T019 == 2 ~ 5,
T001:T019 == 1 ~ 6,
))

Sorry for asking, I don't have much experience with R..

The old data does not have to be saved, but only changed or overwritten, if possible

I now recoded the items with the recode function.

I looks like this:
data_darkmode3$T001 <- recode(data_darkmode3$T001, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T002 <- recode(data_darkmode3$T002, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T003 <- recode(data_darkmode3$T003, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T004 <- recode(data_darkmode3$T004, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T005 <- recode(data_darkmode3$T005, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T006 <- recode(data_darkmode3$T006, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T007 <- recode(data_darkmode3$T007, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T008 <- recode(data_darkmode3$T008, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T009 <- recode(data_darkmode3$T009, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T010 <- recode(data_darkmode3$T010, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T011 <- recode(data_darkmode3$T011, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T012 <- recode(data_darkmode3$T012, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T013 <- recode(data_darkmode3$T013, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T014 <- recode(data_darkmode3$T014, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T015 <- recode(data_darkmode3$T015, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T016 <- recode(data_darkmode3$T016, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T017 <- recode(data_darkmode3$T017, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T018 <- recode(data_darkmode3$T018, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")
data_darkmode3$T019 <- recode(data_darkmode3$T019, "1=6; 2=5; 3=4; 4=1; 5=2; 6=3")

Is this a correct way? It seems like it worked..

Thank you! :slight_smile:

Yes, if that gets to your desired result, then your approach is one way to solve it (there are usually multiple ways to arrive at the same outcome). Nice job working through it!

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.