0

I am working with a survey dataset in R where participants are asked what their present day religion is. I am however only interested if a person is religious or not. There are 14 answer categories in that question with categories 1-8,11,13 and 14 being religious and categories 9,10 and 12 being non-religious. How do I create a new variable where I can cane these subcategories merged so that religious is dispalyed as 1 and non-religious is dispalyed as 0? I have tried it with the ifelse function but that didn`t really work.

Thank you all for your help

lena
  • 1
  • 1
    Please make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) or [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) with a sample input and your expected output. This is needed to create, test and verify possible solutions. – Martin Gal Nov 06 '21 at 12:44

1 Answers1

0

Please find below a reprex which gives you a solution using the data.table library

Reprex

  • Simulated data
set.seed(58) # set seed to make the reprex reproducible!
df <- data.frame(ID = c(seq(1:100)), # ID of participants
                 Answer_cat = c(sample(1:14, 100, replace = TRUE))) # simulated answers
  • Code
library(data.table)
setDT(df)[, Religion_or_Not := fifelse(Answer_cat %in% c(9,10,12),0,1)][]
  • Output
#>       ID Answer_cat Religion_or_Not
#>   1:   1          3               1
#>   2:   2         13               1
#>   3:   3          6               1
#>   4:   4          2               1
#>   5:   5          2               1
#>   6:   6          9               0
#>   7:   7          4               1
#>   8:   8         12               0
#>   9:   9          9               0
#>  10:  10          1               1
#>  11:  11          4               1
#>  12:  12          1               1
#>  13:  13         10               0
#>  14:  14         12               0
#>  15:  15          4               1
#>  16:  16          3               1
#>  17:  17         12               0
#>  18:  18         14               1
#>  19:  19          8               1
#>  20:  20          7               1
#>  21:  21          3               1
#>  22:  22          3               1
#>  23:  23         11               1
#>  24:  24          9               0
#>  25:  25          6               1
#>  26:  26          1               1
#>  27:  27         11               1
#>  28:  28          1               1
#>  29:  29         13               1
#>  30:  30          1               1
#>  31:  31          9               0
#>  32:  32          1               1
#>  33:  33          6               1
#>  34:  34         13               1
#>  35:  35          9               0
#>  36:  36          9               0
#>  37:  37         14               1
#>  38:  38          8               1
#>  39:  39          1               1
#>  40:  40          3               1
#>  41:  41          4               1
#>  42:  42         12               0
#>  43:  43         12               0
#>  44:  44          7               1
#>  45:  45         12               0
#>  46:  46         12               0
#>  47:  47          2               1
#>  48:  48         14               1
#>  49:  49          8               1
#>  50:  50          9               0
#>  51:  51         13               1
#>  52:  52          4               1
#>  53:  53         12               0
#>  54:  54          2               1
#>  55:  55         12               0
#>  56:  56          9               0
#>  57:  57         13               1
#>  58:  58          6               1
#>  59:  59          9               0
#>  60:  60         13               1
#>  61:  61          7               1
#>  62:  62         11               1
#>  63:  63          6               1
#>  64:  64          2               1
#>  65:  65         14               1
#>  66:  66          9               0
#>  67:  67         12               0
#>  68:  68          7               1
#>  69:  69          4               1
#>  70:  70          9               0
#>  71:  71         12               0
#>  72:  72          6               1
#>  73:  73         13               1
#>  74:  74          7               1
#>  75:  75          4               1
#>  76:  76          9               0
#>  77:  77         12               0
#>  78:  78          4               1
#>  79:  79          7               1
#>  80:  80          5               1
#>  81:  81          7               1
#>  82:  82          7               1
#>  83:  83         12               0
#>  84:  84          6               1
#>  85:  85         10               0
#>  86:  86          6               1
#>  87:  87          9               0
#>  88:  88          1               1
#>  89:  89         10               0
#>  90:  90          6               1
#>  91:  91         12               0
#>  92:  92          9               0
#>  93:  93         14               1
#>  94:  94         14               1
#>  95:  95         12               0
#>  96:  96          1               1
#>  97:  97          3               1
#>  98:  98          6               1
#>  99:  99          7               1
#> 100: 100          6               1
#>       ID Answer_cat Religion_or_Not

Created on 2021-11-06 by the reprex package (v2.0.1)

lovalery
  • 4,524
  • 3
  • 14
  • 28
  • Hello @Iena. Welcome to SO! I hope this answer is what you expected. If so, please mark the answer as accepted to make it easier for other SO users to find the right answer. If not, please, tell me what is wrong. Cheers – lovalery Nov 06 '21 at 15:35