0

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
  • A combination of mutate, across and case_when should get you going Commented Aug 8, 2023 at 12:11
  • 1
    Without a worked example it's hard to know exactly what you need, but in your code snippet the output is not assigned to anything. Try dataset <- dataset %>% ,,, and if changing in-situ, look at across() Commented Aug 8, 2023 at 12:12

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.