0

I tried to create a new dummy variable for the vote for a far-right party in european countries from vote for such partis at the level of countries (in national elections) with if else. The problem is that, instead of returning 0 for people that don't meet the condition (namely people that voted for another party) it only returned 1 and NAs (so no 0).

This is what I tried (the dataset is that of the 9th wave of the European Social Survey)

ess$rpopuv <- ifelse(ess$prtvede2 == 6 | ess$prtvtcat == 3 | ess$prtvtdbe == 7 | ess$prtvtdbg == 3 | ess$prtvtdbg == 5, 1, 0)

Thanks !

  • 2
    When you use `==`, if there is an `NA` in your original data you will get `NA` elements in your result vector and this will then propagate through your logicial test. A quick fix is to replace `==` with `%in%` which gives a result only containing `TRUE` or `FALSE`. – rps1227 May 01 '23 at 06:23
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 01 '23 at 15:03
  • Thanks for you help @rps1227 , I tried your solution but now I kind of have the reverse problem as it attributes 0 to respondents instead of NA (as someone who voted in Bulgaria obviously couldn't have voted for a far-right party in Germany) – Oneliner May 02 '23 at 03:01
  • @Oneliner, in that case it would be useful to expand your question and include fictional data to make any other constratins clearer as suggsted by @MrFlick. But of the top of my head, you would need to either generate the variable for each country separately (using something like `group_by()` from the `dplyr` package) or make more detailed logicial selection criteria. – rps1227 May 02 '23 at 06:23

0 Answers0