0

I'm using the Understanding Society Survey to measure racial harassment. The survey is based on two-stage questions, meaning that participants are first asked whether they have been f.ex. attacked (yes/no) and what the reasons for these were (ethnicity, nationality, etc.) and then once they've chosen the reason they are further asked where this form of harassment happened.

I want to create one single variable out of this.

Thus I want to first create one variable meaning that people have been harassed due to their ethnicity. I therefore need to create one variable out of the different locations together (a_resattackedw1_1_3=1 + a_resattackedw1_2_3=1 + ...).

In SPSS I did this like this:

##compute attacked_ethn=0

if(a_resattackedw1_1_3=1) attacked_ethn=1.
if(a_resattackedw1_2_3=1) attacked_ethn=1.
if(a_resattackedw1_3_3=1) attacked_ethn=1.
if(a_resattackedw1_4_3=1) attacked_ethn=1.
if(a_resattackedw1_5_3=1) attacked_ethn=1.
if(a_resattackedw1_6_3=1) attacked_ethn=1.
if(a_resattackedw1_7_3=1) attacked_ethn=1.
if(a_resattackedw1_97_3=1) attacked_ethn=1.

add value labels attacked_ethn 0 'not mentioned' 1 'mentioned'.

How do I do this in R?

And then furthermore how do I create one variable for attacked due to racial reasons after I created the previous variables for all racially motivated reasons?

Again, in SPSS I did it like this:

#compute attacked due to racism variable#

IF  (a_attacked_dv = 1) attacked=SUM(attacked_ethn,attacked_nat,attacked_rel).
VARIABLE LABELS  attacked 'attacked for racial reasons'.
EXECUTE.

FREQUENCIES VARIABLES=attacked
/ORDER=ANALYSIS.

What is the R code for this?

Thank you for your help!

Nora17.06
  • 25
  • 3
  • 1
    Where can we access the database? – TarJae Aug 04 '21 at 18:04
  • The dataset is available via the UK Data Service Website – Nora17.06 Aug 04 '21 at 18:10
  • Link to the database? There is a lot of information! – TarJae Aug 04 '21 at 18:19
  • You can improve your SPSS code like this: `compute attacked_ethn=any(1, a_resattackedw1_1_3, a_resattackedw1_2_3, a_resattackedw1_3_3 ...).` – eli-k Aug 04 '21 at 18:20
  • @TarJae You can only access the data if you have an account with the UK Data Services. But any information about the study or the variables can be found on the Understanding Society Website: https://www.understandingsociety.ac.uk – Nora17.06 Aug 04 '21 at 18:44
  • @eli-k Hi thank you, I appreciate this, but I need the code in R Studio, if you can help with this? – Nora17.06 Aug 04 '21 at 18:49
  • We would need a peace of your data: A good strategy is to `dput(head(your_df))`. See here – TarJae Aug 04 '21 at 18:55

1 Answers1

0

Here's one simple way to make the calculations:

library(dplyr)
yourDF <- yourDF %>% mutate(
    attacked_ethn=1*(
      a_resattackedw1_1_3 == 1 | 
      a_resattackedw1_2_3 == 1 |
      a_resattackedw1_3_3 == 1 |
      a_resattackedw1_4_3 == 1 |
      a_resattackedw1_5_3 == 1 |
      a_resattackedw1_6_3 == 1 |
      a_resattackedw1_7_3 == 1 |
      a_resattackedw1_97_3 == 1)
    ) %>%
 mutate(attacked=if_else(a_attacked_dv == 1, attacked_ethn + attacked_nat + attacked_rel, NULL))

Note this code is very simplistic - you can improve it once you learn more in R language (+ learn to present your data with labels etc' - this is very different than SPSS).

eli-k
  • 10,898
  • 11
  • 40
  • 44