rdplyrrecode

how to apply changes to existing data variables


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.


Solution

  • 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.