Home > Net >  How to mutate conditionally, while keeping original values intact in dplyr?
How to mutate conditionally, while keeping original values intact in dplyr?

Time:09-23

I'm working on my first dataset in R, so please forgive if this is a silly question but I have been struggling with it for days now.

My data looks something like this:

library(tidyverse)
tb1 <- tibble(ID = c(1,2,3,4,5),
       Quest_MF_1_1 = c(1,2,2,1,2),
       Quest_MF_1_2 = c(1,2,2,1,2),
       Quest_MF_2_1 = c(2,3,3,2,1),
       Quest_MF_2_2 = c(1,3,4,1,2),
       Quest_MV_1_1 = c(1,2,4,1,2),
       Quest_MV_1_2 = c(1,2,2,1,2),
       Quest_MV_2_1 = c(2,3,5,2,2),
       Quest_MV_2_2 = c(1,3,4,1,2))

tb_long <- tb1 %>%  pivot_longer(
  cols = 2:8,  
  names_to = c("Questionnaire", "Question", "Timepoint"),
names_pattern = "Quest_(.*)_(.*)_(.*)",
values_to = "score")

I would like to recode the values of all scores of MF_2 and MV_1 (5=1, 4=2, 3=3, 2=4, 1=5), while keeping the other values as they are. I have tried combining mutate and if_else but i haven't been able to figure out how to keep the scores of the other questions intact. Any ideas on how to do this?

In my actual data set i have two questionnaire with 20 and 30 questions each, measured at 20 different timepoints. For about halve of the questions of each questionnaire i need to recode te scores.

CodePudding user response:

In the long data, we can use case_when

tb_long2 <- tb_long %>%
     mutate(score = case_when((Questionnaire == "MF" & Question == 2) |
      (Questionnaire == "MV" & Question == 1) ~ 6- score, TRUE ~ score))

-output

tb_long2
# A tibble: 35 x 6
      ID Quest_MV_2_2 Questionnaire Question Timepoint score
   <dbl>        <dbl> <chr>         <chr>    <chr>     <dbl>
 1     1            1 MF            1        1             1
 2     1            1 MF            1        2             1
 3     1            1 MF            2        1             4
 4     1            1 MF            2        2             5
 5     1            1 MV            1        1             5
 6     1            1 MV            1        2             5
 7     1            1 MV            2        1             2
 8     2            3 MF            1        1             2
 9     2            3 MF            1        2             2
10     2            3 MF            2        1             3
# … with 25 more rows

CodePudding user response:

library(dplyr, warn.conflicts = FALSE)
tb1 <- tibble(ID = c(1,2,3,4,5),
       Quest_MF_1_1 = c(1,2,2,1,2),
       Quest_MF_1_2 = c(1,2,2,1,2),
       Quest_MF_2_1 = c(2,3,3,2,1),
       Quest_MF_2_2 = c(1,3,4,1,2),
       Quest_MV_1_1 = c(1,2,4,1,2),
       Quest_MV_1_2 = c(1,2,2,1,2),
       Quest_MV_2_1 = c(2,3,5,2,2),
       Quest_MV_2_2 = c(1,3,4,1,2))

tb1 %>% 
  mutate(across(starts_with('Quest_MV_1') | starts_with('Quest_MF_2'), 
                ~ recode(.x, '5' = 1, '4' = 2, '3' = 3, '2' = 4, '1' = 5)))
#> # A tibble: 5 × 9
#>      ID Quest_MF_1_1 Quest_MF_1_2 Quest_MF_2_1 Quest_MF_2_2 Quest_MV_1_1
#>   <dbl>        <dbl>        <dbl>        <dbl>        <dbl>        <dbl>
#> 1     1            1            1            4            5            5
#> 2     2            2            2            3            3            4
#> 3     3            2            2            3            2            2
#> 4     4            1            1            4            5            5
#> 5     5            2            2            5            4            4
#> # … with 3 more variables: Quest_MV_1_2 <dbl>, Quest_MV_2_1 <dbl>,
#> #   Quest_MV_2_2 <dbl>

Created on 2021-09-22 by the reprex package (v2.0.1)

CodePudding user response:

tb1[, 2:8] <- 6 - tbl[, 2:8])
  • Related