代码之家  ›  专栏  ›  技术社区  ›  SvW

排名变量(非观察值)

  •  -1
  • SvW  · 技术社区  · 8 年前

    我的调查问卷是从被调查者那里获得的数据,按照对他们重要性的等级对20个项目进行排名。量表的下端包含一个“垃圾桶”,受访者可以扔掉他们认为完全不重要的20个项目中的任何一个。结果是一个包含20个变量的数据集(每个项目1个)。每个变量都会收到一个介于1和100之间的数字(如果该项被扔进bin,则为0)

    我想将条目重新编码为每个受访者的变量排名。因此,所有变量都会得到一个相对于受访者排名的1到20之间的数字。

    例子:

    当前:

                   item1 item2 item3 item4 item5 item6 item7 item8 etc.
    respondent1    67    44    29    7     0     99    35    22
    respondent2    0     42    69    50    12    0     67    100
    etc.
    

    我想要什么:

                   item1 item2 item3 item4 item5 item6 item7 item8 etc.
    respondent1    7     6     4     2     1     8     5     3
    respondent2    1     4     7     5     3     1     6     8
    etc.
    

    正如您在respondent2中看到的那样,我希望收到相同值的项目获得相同的排名,然后跳过一个数字。

    我发现了很多关于如何对观察结果进行排序的信息,但我还没有发现如何对变量进行排序。有人知道怎么做吗?

    1 回复  |  直到 8 年前
        1
  •  1
  •   ander2ed    8 年前

    下面是一个使用 reshape :

    /* Create sample data */
    
    clear *
    set obs 2
    gen respondant = "respondant1"
    replace respondant = "respondant2" in 2
    set seed 123456789
    forvalues i = 1/10 {
        gen item`i' = ceil(runiform()*100)
    }
    replace item2 = item1 if respondant == "respondant2"
    list
    
    
         +----------------------------------------------------------------------------------------------+
         |  respondant   item1   item2   item3   item4   item5   item6   item7   item8   item9   item10 |
         |----------------------------------------------------------------------------------------------|
      1. | respondant1      14      56      69      62      56      26      43      53      22       27 |
      2. | respondant2      65      65      11       7      88       5      90      85      57       95 |
         +----------------------------------------------------------------------------------------------+
    
    
    /* reshape long first */
    reshape long item, i(respondant) j(itemNum)
    
    /* Rank observations, accounting for ties */
    by respondant (item), sort : gen rank = _n
    replace rank = rank[_n-1] if item[_n] == item[_n-1] & _n > 1
    
    /* reshape back to wide format */
    drop item // optional, you can keep and just include in reshape wide
    reshape wide rank, i(respondant) j(itemNum)