How to create a derived variable in R?

Hi all, I have a series of dichotomous questions that could be combined into one categorical variable. (e.g., Original formal: reasons for removal from the program = Dropped out voluntarily (yes/no), removed involuntarily (yes/no). I want to to be one variable that just has categories for dropped out and removed).
How do I do this?

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Hi, thank you for your response! I can't share the actual data but I recreated some fake data that still resembles what I want to do. As for the code, I wasn't even sure what something like this would be called and I couldn't find anything close when I tried searching myself, so I don't have example code I've been using. This is my first time trying to add data, so let me know if I messed up.

tibble::tribble(
  ~`Removal.Reason.-.Voluntary`, ~`Removal.Reason.-.Involuntary`, ~`Removal.Reason.-.Other`,
                             0L,                              0L,                        0L,
                             1L,                              0L,                        0L,
                             0L,                              1L,                        0L,
                             0L,                              0L,                        1L,
                             0L,                              1L,                        0L,
                             1L,                              0L,                        0L,
                             0L,                              1L,                        0L,
                             0L,                              0L,                        1L,
                             0L,                              1L,                        0L,
                             1L,                              0L,                        0L,
                             0L,                              1L,                        0L,
                             0L,                              0L,                        1L,
                             0L,                              1L,                        0L,
                             1L,                              0L,                        0L,
                             0L,                              1L,                        0L,
                             0L,                              0L,                        1L,
                             0L,                              1L,                        0L,
                             1L,                              0L,                        0L,
                             0L,                              1L,                        0L
  )

your image is not copy and pasteable, therefore please dont screenshot text, rather, copy and paste it.
You can format text as code in this forum by using triple backticks like this

```
this is code

Yeah I realized that. I've edited my comment several times now trying to get it to do the right thing. Let's see if this one works.... Sorry! I'm totally new to R as if it wasn't obvious.

tibble::tribble(
  ~`Removal.Reason.-.Voluntary`, ~`Removal.Reason.-.Involuntary`, ~`Removal.Reason.-.Other`,
                             0L,                              0L,                        0L,
                             1L,                              0L,                        0L,
                             0L,                              1L,                        0L,
                             0L,                              0L,                        1L,
                             0L,                              1L,                        0L,
                             1L,                              0L,                        0L,
                             0L,                              1L,                        0L,
                             0L,                              0L,                        1L,
                             0L,                              1L,                        0L,
                             1L,                              0L,                        0L,
                             0L,                              1L,                        0L,
                             0L,                              0L,                        1L,
                             0L,                              1L,                        0L,
                             1L,                              0L,                        0L,
                             0L,                              1L,                        0L,
                             0L,                              0L,                        1L,
                             0L,                              1L,                        0L,
                             1L,                              0L,                        0L,
                             0L,                              1L,                        0L
  )
library(tidyverse)

(mydf <- tibble::tribble(
  ~`rr.Voluntary`, ~`rr.Involuntary`, ~`rr.Other`,
  0L, 0L, 0L,
  1L, 0L, 0L,
  0L, 1L, 0L,
  0L, 0L, 1L,
  0L, 1L, 0L,
  1L, 0L, 0L,
  0L, 1L, 0L,
  0L, 0L, 1L,
  0L, 1L, 0L,
  1L, 0L, 0L,
  0L, 1L, 0L,
  0L, 0L, 1L,
  0L, 1L, 0L,
  1L, 0L, 0L,
  0L, 1L, 0L,
  0L, 0L, 1L,
  0L, 1L, 0L,
  1L, 0L, 0L,
  0L, 1L, 0L
) %>% mutate(new_rr = factor(case_when(
  rr.Voluntary == 1 ~ "Voluntary",
  rr.Involuntary == 1 ~ "Involuntary",
  rr.Other == 1 ~ "Other",
  TRUE ~ "Not even Other"
))))

Thanks! What if realistically, I can't copy and paste the data into my code?

I think you are confused, if you read the reprex guide more carefully, you would realize that including the data into the code is just for the purpose of reproducibility of the example. In your actual application, you just have to use your own data frame instead.

This topic was automatically closed 21 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.