如何从不同格式的数据集中重新编码并创建多个变量?

时间:2019-09-30 21:41:01

标签: r loops dplyr recode

我正在使用在美国(us_data)和在乌克兰(ukr_data)进行的两个不同的调查数据集,这些数据集让受访者评估了他们对政客可以采取的不同行动的反应(在下面的工作示例中分别标记为行动1和行动2) )。受访者从四个维度评估了每个操作:

   1) How acceptable they view the action? 
   2) How violent they think the action is?
   3) How threatened they feel by the action?
   4) How disgusted they feel by the action?

在几个动作中重复1-4的测量。两次调查所问的问题完全相同,但数据集格式不同。乌克兰数据集的格式很长,带有一个变量,用于标记受访者正在评估操作1或操作2。相比之下,美国数据集的格式很宽,在四个维度上的每个操作都有不同的问题。

我希望能够创建一个新变量并将变量重新编码为一个大函数,并为每个动作(动作1和动作2中的动作)返回每次评估的方式(可接受,暴力,威胁和厌恶) (例如该示例)针对每个国家(美国和乌克兰),以及针对每个国家/地区的每项操作进行的每四个评估的Cronbachα。由于实际数据集中有40多个动作,因此最好将其作为循环的一部分进行。

作为R新手,由于数据集的格式不同,我遇到了一些问题,而且我不确定如何重新大规模编码变量。

下面,我提供了两个数据集的最小工作示例。我可以使该代码块正常工作,并重新编码Action 1的可接受性及其在美国和乌克兰的均值。但是我希望能够执行一个循环,对每个动作(40 +),所有4个评估以及每个国家/地区执行此操作。

> library(dplyr)
> library(psych)
> 
> ##Clear the Workspace
> rm(list = ls())
> 
> 
> ##Setting Up the Ukraine data###
> 
> ##Which Action is being addressed
> ukr_actlabels<-c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2)
> 
> ###Attitudes Towards Actions
> ukr_acceptable<-c(1,6, 2,1, 3,1, 4, 1, 2, 5)
> ukr_violent<-c(2,1, 2,1, 3,1, 4, 7, 2, 3)
> ukr_threat<- c(1,1, 1,1, 3,1, 4, 1, 2, 3)
> ukr_disgusting<- c(1,3, 6,1, 3,1, 3, 1, 2, 4)
> 
> ukr_data<-data.frame(cbind(ukr_actlabels,ukr_acceptable, ukr_violent,          ukr_threat, ukr_disgusting  ))
    > ukr_data
   ukr_actlabels ukr_acceptable ukr_violent ukr_threat ukr_disgusting
1              1              1           2          1              1
2              1              6           1          1              3
3              1              2           2          1              6
4              1              1           1          1              1
5              1              3           3          3              3
6              2              1           1          1              1
7              2              4           4          4              3
8              2              1           7          1              1
9              2              2           2          2              2
10             2              5           3          3              4
> 
> ###US data 
> ##Attitudes Towards Action 1
> us_a1_acceptable<-c(3,1, 4, 1, 2, NA, NA, NA, NA, NA)
> us_a1_violent<-c(2,1, 2, 1, 2, NA, NA, NA, NA, NA)
> us_a1_threat<-c(2,1, 5, 1, 1, NA, NA, NA, NA, NA)
> us_a1_disgusting<-c(2,1, 3, 3, 3, NA, NA, NA, NA, NA)
> 
> ##For Action 2
> ##Attitudes Towards action 2
> us_a2_acceptable<-c(NA, NA, NA, NA, NA, 3,1, 4, 2, 5)
> us_a2_violent<-c(NA, NA, NA, NA, NA, 2,1, 2, 1, 2)
> us_a2_threat<-c(NA, NA, NA, NA, NA, 2,1, 5, 1, 3)
> us_a2_disgusting<-c(NA, NA, NA, NA, NA, 2,1, 3, 3, 4)
> 
> us_data<-data.frame(cbind(us_a1_acceptable,us_a1_violent, us_a1_threat,     us_a1_disgusting, us_a2_acceptable,us_a2_violent, us_a2_threat,     us_a2_disgusting))
> us_data
   us_a1_acceptable us_a1_violent us_a1_threat us_a1_disgusting     us_a2_acceptable us_a2_violent
1                 3             2            2                2                       NA            NA
2                 1             1            1                1                NA            NA
3                 4             2            5                3               NA            NA
4                 1             1            1                3               NA            NA
5                 2             2            1                3               NA            NA
6                NA            NA           NA               NA                    3             2
7                NA            NA           NA               NA                1             1
8                NA            NA           NA               NA                4             2
9                NA            NA           NA               NA                2             1
10               NA            NA           NA               NA                5             2
   us_a2_threat us_a2_disgusting
1            NA               NA
2            NA               NA
3            NA               NA
4            NA               NA
5            NA               NA
6             2                2
7             1                1
8             5                3
9             1                3
10            3                4
> 
> ##How acceptable is Action 1 in Ukraine?####
> 
> ##Creating a new variable for acceptable filled with NAs
> ukr_data$act1_acceptable<-NA
> 
> ###Substituting the previous values of ukr_acceptable if 
> ukr_data$act1_acceptable<-ifelse(ukr_data$ukr_actlabels==1,     ukr_data$ukr_acceptable, NA)
> table(ukr_data$act1_acceptable)

1 2 3 6 
2 1 1 1 
> 
> ###Reocding so #1=Never, 2= Rarely, 3= Sometimes, 4=Very Often, 5=Always
> ukr_data$act1_acceptable<-recode(ukr_data$act1_acceptable,`1`=5, `2`=4,     `3`=3, `4`=2, `5`=1)
> table(ukr_data$act1_acceptable)

3 4 5 6 
1 1 2 1 
> 
> ##Recoding the missing values of 6 and 7 to NA
> ukr_data$act1_acceptable[ukr_data$act1_acceptable ==6 | ukr_data$act1_acceptable ==7] <- NA 
> table(ukr_data$act1_acceptable)

3 4 5 
1 1 2 
> 
> ###How acceptable is Action 1 in the US?
> table(us_data$us_a1_acceptable)

1 2 3 4 
2 1 1 1 
> 
> 
> ###Creating a new variable 
> ###Reocding so #1=Never, 2= Rarely, 3= Sometimes, 4=Very Often, 5=Always
> us_data$act1_acceptable<-recode(us_data$us_a1_acceptable,`1`=5, `2`=4,     `3`=3, `4`=2, `5`=1)
> 
> 
> table(us_data$act1_acceptable)

2 3 4 5 
1 1 1 2 
> 
> 
> ####Returning the mean
> 
> mean(ukr_data$act1_acceptable, na.rm=TRUE)
[1] 4.25
> 
> mean(us_data$act1_acceptable, na.rm=TRUE)
[1] 3.8 

我想做的是让循环返回以下信息。对于每个动作(动作1到动作43,我的实际数据集中有43个)。我希望每个操作都返回一个循环:

        1) Mean Acceptability of the Action in the US
        2) Mean Acceptability of the Action in Ukraine
        3) Mean Perception of Violence of the Action in US
        4) Mean Perception of Violence of the Action in Ukraine 
        5) Mean Threat of the Action in US
        6) Mean Threat of the Action in Ukraine
        7) Mean Disgust of the Action in the US
        8) Mean Disgust of the Action in Ukraine
        9)Cronbach's Alpha of Acceptable, Violence, Threat, and Disgust for Action in the US
        10) Cronbach's Alpha of Acceptable, Violence, Threat, and Disgust for Action in Ukraine

我确实有一个电子表格,列出了哪些标签(乌克兰数据集)和问题(美国)数据集对应于哪个操作。

感谢您的帮助! :)

0 个答案:

没有答案