how to apply changes to existing data variables

56 Views Asked by At

I perform lots of data transformation and one of them makes me some troubles.

Let's assume that I have a dataset with variavles from v1 to v100, every one with numbers from 1 to 5. I want to recode/change some of variables (for example from v10 to v20)

I use dplyr package a lot and some another coming from Daniel Ludecke, i.e. sjmisc, sjPlot, sjlabelled etc.

dataset %>%
   select(v10:v20) %>%
   rec(rec = "1:2=1;3:5=2")

How to send the result of this operation to the initial dataset to the same place from which they were retrieved (this will be an overwrite)?

I don't want to create additional variable (i.e. v10_r, v11_r etc), just overwrite. I make a lot of these and similar changes to a dataset and would like to be able to apply them as simply as possible.

2

There are 2 best solutions below

0
pbraeutigm On BEST ANSWER

You can achieve this using the mutate_at function from the dplyr package:

  library(dplyr)

    dataset <- dataset %>%
      mutate_at(vars(v10:v20), ~ recode(.x, `1:2` = 1, `3:5` = 2))

This will modify the original dataset in place, overwriting the variables from v10 to v20 with the recoded values.

0
AndrewGB On

Another option is to use case_match from dplyr, which effectively replaced recode:

library(dplyr)

df %>% 
  mutate(across(v10:v20,
                ~ case_match(.x,
                            1:2 ~ 1,
                            3:5 ~ 2)))

Or you can also use case_when from dplyr, but slightly more verbose than using case_match.

df %>%
  mutate(across(v10:v20,
                ~ case_when(.x %in% (1:2) ~ 1,
                            .x %in% (3:5) ~ 2)))