r-对于我的所有样本,将多方选择变量更改为一个yes no变量



我正在研究国内和东道国选举中的选票,看看散居欧洲的土耳其人。

由于我已经有了母国选举的"是"-"否"变量(意味着你是否投票(,我希望我的数据集的所有东道国都有相同的变量。

事实上,我有下面的变量,每个变量对应一个国家;partyname";或";none";或NA。例如德国D$partyvotedger的";SPD"none";或者NA。它们是字符向量。

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
#

我想把所有四个国家合并成一个变量,编码为";是"否";或NA,其中";是";每当一方名称出现时发生;none";出现。

得到这样的东西:(这是在母国选举中的投票,0=否,1=是(

`D$vote_turkey`
<dbl>
1               0
2               0
3               1
4               1
5               1
6               1
7               1
8               0
9               0
10               1

这里有人知道我应该使用什么函数或代码结构吗?在拥有一个独特的国家之前,我是否需要先为每个国家创建一个"是"-"否"?

提前感谢您的回答。

一切顺利。

这是我的答案。首先,我根据您提供的内容创建一个示例数据集。

library(dplyr)
#creating the example data frame
example <- data.frame("partyvotedgr" = c(NA,                                     
"SPD",                               
"none / not chosen / not going to choose",
NA,
"none / not chosen / not going to choose",
"none / not chosen / not going to choose",
"none / not chosen / not going to choose",
NA,
"none / not chosen / not going to choose",
"none / not chosen / not going to choose",
"CDU",
"DIE LINKE"))
#checking how the table looks like
example
"partyvotedgr
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
11                                     CDU
12                               DIE LINKE"

我注意到你目前关注的是德国,所以我添加了一些额外的政党来更好地模拟。

然后,声明一个字符串向量,该向量包含目标公司中的参与方列表。例如,德国就是这样。

#retrieving only 2 & 3
party_value <- c("SPD", "CDU", "DIE LINKE")

当然,您应该添加更多的值来覆盖所有各方。

然后创建一个类似于vote_turkey的列(vote_ger(。我把NA值作为一个基本值。

#creating the 'vote_ger' column
example <- example %>% 
mutate(vote_ger = NA_character_)

接下来,根据partyvotedgr值中的值更改vote_ger值。

#adjusting the vote_ger values
#creating the yes values according to the party_value
example <- example %>%
mutate(vote_ger=ifelse(partyvotedgr %in% party_value,
"yes",
vote_ger))

请注意,%in%运算符是一个返回布尔值(True或False(的集合运算。在这种情况下,如果partyvotedgr列中的元素是party_value向量的元素,则返回。

您可以执行几乎完全相同的过程,在vote_ger列中给定"no"值。

#creating the no values if they have the value 'none / not chosen / not going to choose' in the partyvotedgr column
example <- example %>%
mutate(vote_ger=ifelse(partyvotedgr %in% "none / not chosen / not going to choose",
"no",
vote_ger))

这一次,如果partyvotedgr列中具有"none / not chosen / not going to choose"值,则vote_ger列将具有"no"

在所有处理之后,数据集example看起来是这样的。

#the result
example
"partyvotedgr vote_ger
1                                     <NA>     <NA>
2                                      SPD      yes
3  none / not chosen / not going to choose       no
4                                     <NA>     <NA>
5  none / not chosen / not going to choose       no
6  none / not chosen / not going to choose       no
7  none / not chosen / not going to choose       no
8                                     <NA>     <NA>
9  none / not chosen / not going to choose       no
10 none / not chosen / not going to choose       no
11                                     CDU      yes
12                               DIE LINKE      yes"

解决方案在base-r中非常简单,因为r为您矢量化比较运算符。

如果";否";值总是相同的字符串,则!=就足够了:

example$vote <- as.numeric(example$partyvotedgr != "none / not chosen / not going to choose")
example
partyvotedgr vote
1                                     <NA>   NA
2                                      SPD    1
3  none / not chosen / not going to choose    0
4                                     <NA>   NA
5  none / not chosen / not going to choose    0
6  none / not chosen / not going to choose    0
7  none / not chosen / not going to choose    0
8                                     <NA>   NA
9  none / not chosen / not going to choose    0
10 none / not chosen / not going to choose    0
11                                     CDU    1
12                               DIE LINKE    1

!=比较返回TRUE、FALSE和NA的数组,as.numeric将TRUE/FALSE转换为1/0。考虑是否最好将它们保持为TRUE/FALSE。

如果存在几个选项;否";,操作员将是%in%,就像在(!example$partyvotedgr %in% c("none", "not chosen", "not going to choose"))中一样

感谢B_Heidel的示例df。

相关内容

最新更新