I am studying the votes in home and host country elections looking at the turkish diaspora in Europe.
Since I already have a yes-no variable (meaning did you vote or not) for the home country election, I'd like to have the same for all host countries of my dataset.
As it is I have the following variables each for one country coded "partyname" or "none" or NA. Example for Germany D$partyvotedger its either "SPD", "none" or NA. They are character vectors.
1 NA
2 SPD
3 none / not chosen / not going to choose
4 NA
5 none / not chosen / not going to choose
6 none / not chosen / not going to choose
7 none / not chosen / not going to choose
8 NA
9 none / not chosen / not going to choose
10 none / not chosen / not going to choose
# … with 2,347 more rows
#
I'd like to combine all four countries into one variable coded "yes" "no" or NA where "yes" occurs whenever a party name appears and no whenever "none" appears.
Get something like this: (This the vote in home country election where 0= no and 1= yes)
`D$vote_turkey`
<dbl>
1 0
2 0
3 1
4 1
5 1
6 1
7 1
8 0
9 0
10 1
Has anyone here an Idea of what function or code structure I should use ? Do I need to first create a yes-no for each country before having a unique one ?
Thanks in advance for your answer.
All the best.
Here is my answer. First I create an example data set based on what you have provided.
I noticed that you're currently focusing on Germany so I added some additional political parties for better simulation.
Then, you declare a string vector that contains a list of parties in a target company. For instance, it would be like this for Germany.
Of course you should add more values to cover all the parties.
Then you create a similar column(
vote_ger) like thevote_turkey. I give the NA value as a base value.Next, you change the
vote_gervalues according to the values in thepartyvotedgrvalues.Please note that
%in%operator is a set operation returning boolean values(either True or False). In this case, it is returning if an element in thepartyvotedgrcolumn is an element of theparty_valuevector.You do almost the exact same process to give
"no"values in thevote_gercolumn.This time,
vote_gercolumn will have a"no"if it has"none / not chosen / not going to choose"value in thepartyvotedgrcolumn.After all the process the data set
examplelooks like this.