简体   繁体   English

如何将联合数据从宽到长重塑?

[英]How to reshape conjoint data from wide to long?

users,用户,

I have received a data from a conjoint survey experiment.我收到了来自联合调查实验的数据。 What I want to do is to reshape from wide to long format.我想做的是从宽格式到长格式重塑。 However, this seems to be slightly complicated.但是,这似乎有点复杂。 I am pretty sure it is possible to do with cj_tidy (package cregg ) but can't solve it myself.我很确定可以使用cj_tidy (包cregg ),但自己无法解决。

In the survey, the respondents were asked to compare two organizations that vary across 7 profiles ( Efficiency Opennes Inclusion Leader Gain & System ).在调查中,受访者被要求比较两个组织,它们在 7 个配置文件中有所不同(效率开放包容领导者增益系统)。 In total, respondents were presented with four comparisons.总共向受访者提供了四个比较。 So 2 organizations and 4 comparisons (4x2).所以 2 个组织和 4 个比较 (4x2)。 They had to choose one of the presented organization and rate them separately after choosing one.他们必须选择其中一个组织,并在选择一个后分别对它们进行评分。

At the moment, the profile variables are structured in this way: org1_Efficiency_conj_1, org1_Opennes_conj1..etc.目前,配置文件变量的结构如下:org1_Efficiency_conj_1、org1_Opennes_conj1..etc。 The first part "org" indicates whether it is the first or second organization.第一部分“org”表示它是第一组织还是第二组织。 The last part "conj", indicated the order of the conjoint/comparison, where the "conj4" is the last comparison.最后一部分“conj”,表示联合/比较的顺序,其中“conj4”是最后一个比较。 The CHOICE variables also follow the order of conjoint – for example,"CHOICE_conj1", "CHOICE_conj2", where =1 means the respondent chose "org1". CHOICE 变量也遵循联合的顺序——例如,“CHOICE_conj1”、“CHOICE_conj2”,其中 =1 表示受访者选择了“org1”。 If =2, then org2 was chosen.如果 =2,则选择 org2。 The RATING> variable indicates a value from 0 to 10 for each organization: RATING_conj1_org1; RATING> 变量表示每个组织的 0 到 10 的值:RATING_conj1_org1; RATING_conj1_org2 etc.. RATING_conj1_org2 等。

The current wide format of the data is not suitable for conjoint analysis - what I need is to create 8 observations for each respondent (4x2=8) where the variable CHOICE would indicate which of the organizations were chosen (where =1 if yes; and =0 if no).当前广泛的数据格式不适合联合分析 - 我需要为每个受访者(4x2=8)创建 8 个观察值,其中变量CHOICE将指示选择了哪些组织(如果是,则 =1;和= 0,如果没有)。 In a similar way, the variable RATING should indicate the rating given by respondents for both of the organizations (0 to 10).以类似的方式,变量RATING应指示受访者对两个组织的评分(0 到 10)。

This is how I would like the data to look like:这就是我希望数据的样子:

在此处输入图像描述

Note please that there are also covariates such as Q1 and Q2 in the picture, they are not a part of the experiment and should remain constant for each individual observation.请注意,图中还有 Q1 和 Q2 等协变量,它们不是实验的一部分,对于每个单独的观察都应保持不变。

Below I share 50 observations from my real data.下面我分享我的真实数据中的 50 个观察结果。

    > dput(cjdata_wide) structure(list(ID = 1:50, org1_Effeciency_conj_1 =
    > c(3L, 2L,  1L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 3L,
    > 2L,  3L, 3L, 3L, 2L, 3L, 1L, 2L, 1L, 3L, 3L, 1L, 1L, 3L, 1L, 1L, 3L, 
    > 3L, 2L, 3L, 2L, 3L, 2L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 2L, 2L, 1L ),
    > org1_Oppenes_conj_1 = c(3L, 3L, 1L, 3L, 1L, 3L, 2L, 3L, 2L,  3L, 1L,
    > 1L, 1L, 2L, 3L, 2L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 3L, 3L,  3L, 1L, 3L,
    > 1L, 2L, 2L, 3L, 2L, 2L, 1L, 3L, 1L, 3L, 2L, 2L, 1L,  2L, 3L, 3L, 3L,
    > 3L, 3L, 2L, 3L, 1L), org1_Inclusion_conj_1 = c(2L,  1L, 1L, 2L, 2L,
    > 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,  2L, 1L, 1L, 1L, 1L,
    > 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L,  1L, 1L, 1L, 1L, 1L, 1L,
    > 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L,  2L), org1_Leader_conj_1 =
    > c(5L, 6L, 3L, 6L, 1L, 4L, 2L, 6L, 1L,  6L, 1L, 2L, 2L, 6L, 3L, 2L, 6L,
    > 3L, 5L, 6L, 3L, 1L, 4L, 3L, 5L,  5L, 2L, 1L, 4L, 1L, 3L, 4L, 2L, 3L,
    > 5L, 2L, 1L, 3L, 3L, 2L, 1L,  4L, 1L, 5L, 2L, 6L, 1L, 4L, 2L, 3L),
    > org1_Gain_conj_1 = c(4L,  4L, 1L, 3L, 3L, 8L, 3L, 2L, 6L, 5L, 1L, 6L,
    > 3L, 8L, 1L, 3L, 6L,  2L, 2L, 5L, 5L, 3L, 4L, 8L, 6L, 4L, 5L, 6L, 6L,
    > 8L, 4L, 4L, 5L,  7L, 6L, 7L, 3L, 7L, 8L, 2L, 6L, 4L, 6L, 4L, 8L, 4L,
    > 6L, 4L, 3L,  6L), org1_System_conj_1 = c(5L, 4L, 5L, 1L, 4L, 4L, 5L,
    > 1L, 2L,  2L, 4L, 3L, 1L, 4L, 4L, 2L, 3L, 3L, 2L, 4L, 3L, 1L, 4L, 3L,
    > 1L,  1L, 5L, 3L, 1L, 3L, 5L, 4L, 5L, 3L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 
    > 1L, 3L, 5L, 5L, 5L, 1L, 1L, 5L, 3L), org2_Effeciency_conj_1 = c(2L, 
    > 1L, 3L, 2L, 1L, 3L, 1L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 2L, 2L,  1L,
    > 2L, 2L, 2L, 3L, 1L, 3L, 1L, 3L, 2L, 1L, 2L, 2L, 1L, 2L, 3L,  1L, 2L,
    > 1L, 1L, 3L, 2L, 1L, 3L, 3L, 2L, 3L, 3L, 2L, 2L, 3L, 3L,  3L),
    > org2_Oppenes_conj_1 = c(1L, 1L, 3L, 1L, 3L, 1L, 1L, 2L,  3L, 2L, 3L,
    > 3L, 2L, 1L, 1L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,  1L, 2L, 3L, 1L,
    > 2L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 3L, 1L,  2L, 3L, 1L, 1L, 1L,
    > 2L, 1L, 1L, 1L, 3L), org2_Inclusion_conj_1 = c(1L,  2L, 2L, 1L, 1L,
    > 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L,  1L, 2L, 2L, 2L, 2L,
    > 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L,  2L, 2L, 2L, 2L, 2L, 2L,
    > 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L,  1L), org2_Leader_conj_1 =
    > c(4L, 5L, 6L, 3L, 2L, 5L, 1L, 3L, 6L,  2L, 4L, 6L, 6L, 5L, 6L, 4L, 1L,
    > 2L, 4L, 2L, 4L, 6L, 5L, 6L, 4L,  1L, 3L, 5L, 3L, 5L, 6L, 1L, 6L, 4L,
    > 1L, 3L, 4L, 2L, 1L, 3L, 4L,  3L, 5L, 2L, 4L, 4L, 3L, 3L, 4L, 2L),
    > org2_Gain_conj_1 = c(5L,  1L, 6L, 5L, 8L, 6L, 4L, 3L, 8L, 8L, 7L, 7L,
    > 7L, 5L, 7L, 7L, 2L,  6L, 7L, 7L, 6L, 8L, 3L, 1L, 8L, 2L, 6L, 2L, 5L,
    > 6L, 7L, 1L, 7L,  2L, 2L, 5L, 8L, 6L, 2L, 7L, 8L, 7L, 1L, 8L, 4L, 3L,
    > 4L, 7L, 7L,  7L), org2_System_conj_1 = c(3L, 3L, 3L, 4L, 3L, 3L, 3L,
    > 5L, 4L,  4L, 1L, 4L, 3L, 1L, 5L, 5L, 5L, 4L, 3L, 3L, 4L, 4L, 1L, 5L,
    > 5L,  3L, 4L, 2L, 5L, 2L, 2L, 5L, 3L, 4L, 3L, 5L, 5L, 5L, 5L, 2L, 3L, 
    > 4L, 2L, 1L, 3L, 3L, 2L, 4L, 4L, 2L), org1_Effeciency_conj_2 = c(2L, 
    > 1L, 2L, 3L, 3L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L, 3L, 3L, 6L, 7L, 1L, 3L, 6L, 5L, 7L, 6L, 6L,
    > 3L, 4L, 2L,  6L), org1_System_conj_2 = c(5L, 1L, 5L, 1L, 4L, 3L, 3L,
    > 4L, 2L,  1L, 5L, 3L, 5L, 3L, 4L, 2L, 2L, 3L, 4L, 1L, 1L, 4L, 3L, 4L,
    > 3L,  2L, 1L, 1L, 4L, 5L, 2L, 3L, 5L, 3L, 5L, 2L, 4L, 2L, 1L, 5L, 5L, 
    > 1L, 2L, 2L, 5L, 2L, 4L, 3L, 2L, 3L), org2_Effeciency_conj_2 = c(3L, 
    > 3L, 1L, 2L, 2L, 1L, 3L, 1L, 3L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 3L,  1L,
    > 2L, 3L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 2L, 3L,  3L, 3L,
    > 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L,  1L),
    > org2_Oppenes_conj_2 = c(2L, 2L, 1L, 3L, 1L, 1L, 1L, 1L,  3L, 2L, 2L,
    > 3L, 3L, 1L, 2L, 1L, 2L, 3L, 3L, 3L, 3L, 2L, 3L, 1L,  1L, 3L, 1L, 3L,
    > 2L, 2L, 2L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 2L, 3L,  2L, 2L, 2L, 2L, 2L,
    > 2L, 2L, 1L, 1L, 2L), org2_Inclusion_conj_2 = c(1L,  2L, 2L, 1L, 2L,
    > 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L,  1L, 2L, 1L, 1L, 1L,
    > 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L,  2L, 1L, 2L, 1L, 1L, 1L,
    > 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L,  1L), org2_Leader_conj_2 =
    > c(6L, 6L, 1L, 4L, 1L, 4L, 4L, 1L, 4L,  4L, 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  2L, 1L, 2L, 2L, 3L,
    > 3L, 2L, 3L, 3L, 3L), org1_Inclusion_conj_3 = c(1L,  1L, 1L, 2L, 1L,
    > 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L,  2L, 1L, 2L, 2L, 2L,
    > 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,  2L, 2L, 1L, 2L, 1L, 1L,
    > 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L,  1L), org1_Leader_conj_3 =
    > c(3L, 1L, 5L, 6L, 3L, 2L, 2L, 6L, 4L,  3L, 3L, 2L, 2L, 1L, 2L, 3L, 5L,
    > 6L, 4L, 1L, 2L, 4L, 5L, 1L, 2L,  2L, 2L, 6L, 4L, 6L, 4L, 6L, 1L, 1L,
    > 3L, 5L, 4L, 1L, 3L, 6L, 2L,  6L, 6L, 1L, 2L, 2L, 6L, 2L, 6L, 5L),
    > org1_Gain_conj_3 = c(2L,  7L, 2L, 4L, 6L, 7L, 2L, 4L, 1L, 5L, 5L, 7L,
    > 5L, 7L, 7L, 3L, 2L,  6L, 2L, 5L, 6L, 6L, 7L, 3L, 5L, 6L, 3L, 8L, 1L,
    > 2L, 8L, 5L, 2L,  8L, 5L, 6L, 5L, 2L, 5L, 3L, 3L, 2L, 4L, 2L, 4L, 5L,
    > 7L, 6L, 2L,  7L), org1_System_conj_3 = c(5L, 5L, 1L, 1L, 4L, 3L, 1L,
    > 1L, 2L,  5L, 1L, 5L, 2L, 1L, 5L, 4L, 1L, 1L, 3L, 4L, 5L, 1L, 5L, 3L,
    > 3L,  5L, 1L, 3L, 2L, 5L, 2L, 1L, 5L, 1L, 3L, 2L, 5L, 5L, 2L, 1L, 3L, 
    > 2L, 2L, 4L, 4L, 4L, 2L, 3L, 5L, 4L), org2_Effeciency_conj_3 = c(2L, 
    > 1L, 2L, 2L, 1L, 2L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 2L, 1L, 1L, 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
    > 5L, 4L, 4L,  5L), org2_System_conj_3 = c(4L, 1L, 4L, 3L, 3L, 5L, 3L,
    > 3L, 4L,  2L, 3L, 1L, 1L, 5L, 2L, 3L, 3L, 2L, 5L, 3L, 1L, 2L, 3L, 5L,
    > 1L,  4L, 5L, 2L, 3L, 2L, 3L, 2L, 4L, 3L, 5L, 3L, 1L, 1L, 3L, 2L, 4L, 
    > 5L, 5L, 3L, 1L, 1L, 4L, 1L, 4L, 5L), org1_Effeciency_conj_4 = c(1L, 
    > 1L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 2L,  3L,
    > 3L, 1L, 1L, 3L, 1L, 3L, 2L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 1L,  3L, 2L,
    > 3L, 3L, 2L, 3L, 1L, 2L, 2L, 3L, 2L, 1L, 1L, 3L, 3L, 1L,  3L),
    > org1_Oppenes_conj_4 = c(2L, 1L, 2L, 2L, 2L, 3L, 2L, 3L,  2L, 1L, 1L,
    > 1L, 3L, 1L, 3L, 2L, 2L, 3L, 2L, 3L, 1L, 3L, 3L, 1L,  1L, 1L, 3L, 1L,
    > 1L, 1L, 2L, 3L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 3L,  1L, 3L, 3L, 1L, 3L,
    > 3L, 3L, 2L, 3L, 2L), org1_Inclusion_conj_4 = c(2L,  2L, 1L, 2L, 2L,
    > 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L,  2L, 2L, 2L, 2L, 1L,
    > 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L,  1L, 2L, 2L, 1L, 2L, 1L,
    > 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L,  1L), org1_Leader_conj_4 =
    > c(4L, 6L, 5L, 1L, 2L, 1L, 1L, 3L, 3L,  6L, 2L, 5L, 6L, 6L, 6L, 2L, 3L,
    > 3L, 4L, 4L, 4L, 1L, 5L, 5L, 2L,  6L, 2L, 5L, 4L, 4L, 2L, 5L, 6L, 5L,
    > 1L, 4L, 4L, 3L, 4L, 2L, 3L,  2L, 5L, 1L, 3L, 6L, 2L, 6L, 4L, 1L),
    > org1_Gain_conj_4 = c(3L,  1L, 2L, 3L, 4L, 7L, 2L, 7L, 4L, 1L, 6L, 3L,
    > 5L, 8L, 3L, 7L, 8L,  1L, 3L, 6L, 7L, 1L, 1L, 1L, 1L, 3L, 4L, 3L, 1L,
    > 8L, 3L, 2L, 1L,  7L, 2L, 4L, 4L, 1L, 6L, 8L, 6L, 3L, 7L, 3L, 8L, 7L,
    > 3L, 1L, 3L,  3L), org1_System_conj_4 = c(5L, 1L, 2L, 3L, 2L, 5L, 5L,
    > 2L, 3L,  5L, 3L, 4L, 5L, 2L, 4L, 2L, 3L, 2L, 4L, 4L, 1L, 1L, 4L, 3L,
    > 2L,  4L, 3L, 1L, 5L, 5L, 2L, 4L, 5L, 4L, 3L, 3L, 1L, 5L, 4L, 1L, 2L, 
    > 3L, 5L, 5L, 3L, 2L, 5L, 2L, 3L, 3L), org2_Effeciency_conj_4 = c(3L, 
    > 3L, 3L, 1L, 1L, 3L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 3L, 2L, 1L,  1L,
    > 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 3L, 2L, 2L, 1L, 3L,  1L, 3L,
    > 2L, 2L, 3L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 3L, 2L, 2L, 3L,  1L),
    > org2_Oppenes_conj_4 = c(1L, 3L, 1L, 3L, 3L, 2L, 3L, 2L,  3L, 2L, 2L,
    > 3L, 2L, 2L, 2L, 1L, 3L, 1L, 3L, 2L, 2L, 1L, 1L, 3L,  3L, 2L, 1L, 3L,
    > 3L, 2L, 3L, 1L, 3L, 3L, 2L, 1L, 3L, 1L, 3L, 1L,  2L, 2L, 1L, 2L, 1L,
    > 1L, 2L, 3L, 1L, 1L), org2_Inclusion_conj_4 = c(1L,  1L, 2L, 1L, 1L,
    > 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L,  1L, 1L, 1L, 1L, 2L,
    > 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L,  2L, 1L, 1L, 2L, 1L, 2L,
    > 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L,  2L), org2_Leader_conj_4 =
    > c(1L, 5L, 2L, 6L, 6L, 6L, 2L, 1L, 2L,  4L, 5L, 3L, 4L, 4L, 2L, 1L, 6L,
    > 1L, 1L, 2L, 6L, 3L, 1L, 4L, 4L,  3L, 3L, 4L, 6L, 5L, 3L, 2L, 3L, 6L,
    > 6L, 5L, 2L, 6L, 3L, 5L, 5L,  1L, 6L, 5L, 4L, 5L, 1L, 2L, 2L, 6L),
    > org2_Gain_conj_4 = c(5L,  8L, 1L, 2L, 7L, 2L, 7L, 8L, 2L, 6L, 7L, 7L,
    > 7L, 5L, 8L, 4L, 6L,  6L, 6L, 4L, 6L, 6L, 7L, 2L, 5L, 6L, 6L, 1L, 8L,
    > 5L, 2L, 5L, 6L,  3L, 3L, 7L, 7L, 8L, 4L, 7L, 5L, 2L, 2L, 7L, 6L, 4L,
    > 7L, 4L, 4L,  1L), org2_System_conj_4 = c(2L, 3L, 3L, 2L, 4L, 4L, 4L,
    > 4L, 1L,  4L, 1L, 2L, 4L, 5L, 2L, 3L, 5L, 1L, 1L, 1L, 5L, 4L, 2L, 2L,
    > 3L,  2L, 1L, 4L, 3L, 4L, 5L, 3L, 1L, 3L, 2L, 4L, 4L, 1L, 3L, 3L, 4L, 
    > 5L, 4L, 4L, 1L, 1L, 3L, 5L, 5L, 1L), CHOICE_conj1 = c(2L, 2L,  1L, 2L,
    > 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,  1L, 1L, 2L,
    > 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L,  1L, 1L, 1L, 1L,
    > 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L ), RATING_conj1_org1 =
    > c(1L, 3L, 6L, 5L, 3L, 1L, 5L, 2L, 0L,  7L, 6L, 8L, 5L, 10L, 8L, 10L,
    > 1L, 6L, 5L, 8L, 2L, 7L, 0L, 6L,  8L, 0L, 4L, 2L, 8L, 6L, 7L, 7L, 7L,
    > 2L, 3L, 8L, 6L, 7L, 2L, 7L,  3L, 8L, 5L, 7L, 8L, 6L, 6L, 10L, 3L, 9L),
    > RATING_conj1_org2 = c(7L,  6L, 4L, 7L, 7L, 1L, 6L, 6L, 0L, 3L, 2L, 0L,
    > 0L, 9L, 5L, 3L, 1L,  6L, 8L, 5L, 2L, 2L, 0L, 4L, 5L, 0L, 6L, 8L, 3L,
    > 5L, 6L, 6L, 5L,  8L, 3L, 8L, 3L, 1L, 5L, 9L, 7L, 3L, 7L, 6L, 6L, 4L,
    > 4L, 0L, 6L,  7L), CHOICE_conj2 = c(1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L,
    > 1L,  2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    > 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L,  2L,
    > 2L, 1L, 1L, 2L, 1L, 1L, 2L), RATING_conj2_org1 = c(5L, 4L,  4L, 4L,
    > 5L, 1L, 5L, 7L, 0L, 3L, 5L, 6L, 5L, 9L, 5L, 3L, 1L, 4L,  4L, 8L, 3L,
    > 7L, 0L, 9L, 9L, 1L, 3L, 2L, 3L, 5L, 6L, 4L, 5L, 8L,  3L, 7L, 6L, 1L,
    > 7L, 0L, 7L, 6L, 6L, 8L, 9L, 7L, 5L, 10L, 7L,  7L), RATING_conj2_org2 =
    > c(0L, 2L, 7L, 4L, 8L, 1L, 7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L, 1L,  2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L),
    > RATING_conj4_org1 = c(4L,  5L, 8L, 6L, 4L, 1L, 8L, 3L, 0L, 7L, 5L, 5L,
    > 2L, 8L, 7L, 10L,  1L, 5L, 5L, 10L, 1L, 3L, 0L, 6L, 7L, 1L, 2L, 5L, 7L,
    > 8L, 7L,  3L, 6L, 2L, 2L, 8L, 5L, 5L, 4L, 5L, 3L, 7L, 3L, 8L, 8L, 6L,
    > 2L,  10L, 7L, 7L), RATING_conj4_org2 = c(6L, 4L, 4L, 4L, 5L, 1L, 6L, 
    > 7L, 0L, 3L, 6L, 2L, 0L, 5L, 5L, 3L, 0L, 3L, 4L, 9L, 4L, 8L, 0L,  5L,
    > 6L, 2L, 8L, 3L, 2L, 5L, 5L, 7L, 2L, 6L, 7L, 8L, 3L, 3L, 1L,  5L, 7L,
    > 10L, 7L, 10L, 5L, 5L, 7L, 5L, 5L, 8L), Q7 = c(0L, 0L,  8L, 9L, 6L,
    > 10L, 2L, 2L, 6L, 8L, 0L, 0L, 5L, 2L, 7L, 7L, 3L,  0L, 0L, 5L, 6L, 4L,
    > 7L, 2L, 977L, 0L, 6L, 3L, 2L, 4L, 7L, 8L,  2L, 1L, 9L, 8L, 10L, 6L,
    > 0L, 9L, 5L, 0L, 3L, 0L, 0L, 0L, 2L,  5L, 977L, 2L), Q8 = c(1L, 1L, 2L,
    > 2L, 2L, 2L, 2L, 1L, 2L, 2L,  1L, 1L, 977L, 1L, 2L, 2L, 1L, 3L, 1L, 1L,
    > 3L, 1L, 3L, 1L, 2L,  1L, 977L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L,
    > 3L, 3L, 2L,  3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 977L, 1L), Q9 = c(4L, 8L,
    > 1L,  0L, 4L, 0L, 8L, 7L, 0L, 0L, 10L, 10L, 0L, 4L, 0L, 10L, 4L, 5L, 
    > 10L, 8L, 2L, 9L, 0L, 5L, 2L, 0L, 5L, 4L, 4L, 8L, 0L, 0L, 5L,  6L, 2L,
    > 0L, 0L, 0L, 7L, 4L, 5L, 5L, 6L, 10L, 7L, 4L, 6L, 0L,  977L, 7L), Q10 =
    > c(8L, 10L, 7L, 5L, 7L, 2L, 7L, 8L, 0L, 2L, 10L,  10L, 0L, 10L, 2L,
    > 10L, 8L, 8L, 10L, 8L, 7L, 10L, 5L, 7L, 4L,  0L, 7L, 7L, 10L, 10L, 4L,
    > 2L, 5L, 9L, 5L, 6L, 2L, 4L, 10L, 3L,  5L, 7L, 9L, 10L, 10L, 10L, 8L,
    > 977L, 977L, 10L), Q11 = c(10L,  9L, 1L, 4L, 5L, 0L, 5L, 6L, 1L, 3L,
    > 9L, 10L, 0L, 10L, 7L, 7L,  5L, 7L, 10L, 10L, 9L, 7L, 0L, 8L, 7L, 0L,
    > 7L, 7L, 8L, 10L, 5L,  2L, 2L, 10L, 5L, 1L, 2L, 4L, 6L, 4L, 7L, 10L,
    > 6L, 8L, 8L, 6L,  8L, 6L, 977L, 10L), Q12 = c(0L, 0L, 0L, 5L, 1L, 10L,
    > 2L, 0L,  0L, 2L, 0L, 0L, 5L, 0L, 6L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
    > 0L,  0L, 10L, 3L, 0L, 0L, 977L, 10L, 7L, 0L, 0L, 5L, 8L, 2L, 0L, 966L,
    > 7L, 977L, 0L, 0L, 0L, 0L, 0L, 0L, 977L, 977L, 0L), Q13 = c(2L,  2L,
    > 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,  2L, 2L,
    > 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L,  2L, 2L, 2L,
    > 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 977L,  2L), Q14 =
    > c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,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1L, 1L ), educ = c(6L, 5L, 2L, 5L, 6L, 6L, 4L, 6L,
    > 3L, 5L, 4L, 5L, 6L,  4L, 4L, 6L, 6L, 6L, 3L, 6L, 5L, 6L, 5L, 5L, 3L,
    > 4L, 6L, 6L, 5L,  3L, 3L, 4L, 3L, 6L, 3L, 5L, 5L, 6L, 3L, 5L, 3L, 3L,
    > 3L, 3L, 4L,  5L, 5L, 4L, 2L, 3L)), class = "data.frame", row.names =
    > c(NA, 
    > -50L))

What I have done so far is this:到目前为止我所做的是:

library(cregg) 
str(long <- cj_tidy(cjdata_wide,
                    profile_variables = c("All the profile variables"),
                    task_variables = c("CHOICE AND RATING VARIABLES HERE"),
                    id = ~ id))
stopifnot(nrow(long) == nrow(data)*4*2

But I'm keep getting errors.但我不断收到错误。 I have tried to follow the example given by the cregg package - but with no success.我试图遵循cregg package 给出的示例 - 但没有成功。 Any help is much appreciated, I am open to all possible ways, be it so through cregg package or tidyr for instance.非常感谢任何帮助,我对所有可能的方式持开放态度,例如通过 cregg package 或tidyr

Your data not being in a standard form mades this a difficult problem.您的数据不是标准格式,这使这成为一个难题。 Here is a solution using the tidyr package.这是使用 tidyr package 的解决方案。

The solutions involves 3 parts, dealing with the profiles, the rating and finally the rating choice.解决方案涉及 3 个部分,处理配置文件、评级和最后的评级选择。

The key to the profiles part was to pivot long and breaking up the profile names into component parts and then pivot wider for the column headings.配置文件部分的关键是长 pivot 并将配置文件名称分解为组件部分,然后为列标题加宽 pivot。

The rating and binary choice involved pivoting longer and then aligning the rows.评级和二元选择涉及旋转更长的时间,然后对齐行。

library(tidyr)
library(dplyr)

#Get the  categories part correct
answer <-cjdata_wide %>% pivot_longer(cols=starts_with("org"), names_to=c("org", "Cat", "conj", "order"), values_to= "values", names_sep="_") %>% select(-c("conj"))
answer <-answer %>% select(!starts_with("RATING") & !starts_with("CHOICE"))
answer <-pivot_wider(answer, names_from = "Cat", values_from = "values") 

#get the ratings column corretn
rating <-cjdata_wide %>% select(starts_with("RATING") )
rating <- rating%>% pivot_longer(cols=everything(), names_to=c("Rating", "conj", "order"), values_to= "Choice_Rating", names_sep="_") %>% select(-c("conj"))

answer$Choice_Rating <- rating$Choice_Rating

#Get the choice correct
choice <-cjdata_wide %>% select(starts_with("CHOICE") )
choiceRate <- choice%>% pivot_longer(cols=everything(), names_to=c("Choice", "conj"), values_to= "Choice_Rating", names_sep="_") %>% select(-c("conj"))

answer$Choice_binary <-ifelse(substr(answer$org, 4,4) == rep(choiceRate$Choice_Rating,each=2), 1, 0)

    answer

It may be possible to simplify the above.可以简化上述内容。 Good luck.祝你好运。

Update per Comment每条评论更新
The final data frame has pairs of rows which corresponds to org 1 or 2. I duplicated the choice so that Choice_Rating column is the same length as the Organization ("org" column).最终的数据框有成对的行,对应于组织 1 或 2。我复制了选择,以便 Choice_Rating 列与组织(“组织”列)的长度相同。 I then compared Choice_Rating & Organization and setting the final value to either 0 or 1 depending on the match.然后我比较了 Choice_Rating & Organization 并根据匹配将最终值设置为 0 或 1。
For question in the comment, A simple way is to convert the factor column to integers with as.integer() function, then the first factor becomes 1 and the second becomes 2 etc. (may need to relevel in order to get the proper order).对于评论中的问题,一种简单的方法是使用as.integer() function 将因子列转换为整数,然后第一个因子变为 1,第二个变为 2 依此类推(可能需要重新调整以获得正确的顺序)。
Another option is to create a new "org" column with your factor names properly listed.另一种选择是创建一个新的“组织”列,并正确列出您的因素名称。
Hopefully this provides enough guidance.希望这能提供足够的指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM