【问题标题】:Analyze ranked-choice data in R在 R 中分析排名选择数据
【发布时间】:2022-11-25 18:53:53
【问题描述】:

我目前正在研究并收集排名选择数据。基本上人们在一个主题中选择他们的偏好。 例如,人们对水果的偏好排序:橙子、芒果、苹果、鳄梨

干净的数据框如下所示:

            Fruits                          Color
1   orange;apple;banana;avocado     blue;yellow;red;green
2   avocado;apple;banana;orange     red;green;blue;yellow
3   apple;banana;orange;avocado     yellow;red;green;blue
4   banana;orange;apple;avocado     green;blue;red;yellow
5   apple;avocado;banana;orange     yellow;blue;yellow;red

第一个人将橙色作为他们的第一偏好,然后将苹果、香蕉和鳄梨作为最后的偏好。 等等

计分:第一偏好 = 4;第二偏好 = 3;第三偏好 = 2;第四偏好 = 1

期望的结果

        apple   avocado banana  orange  blue    green   red yellow
    1   3       1       2       4       4       1       2       3
    2   3       4       2       1       2       3       4       1
    3   4       1       3       2       1       2       3       4
    4   2       1       4       3       3       4       2       1
    5   4       3       2       1       3       2       1       4

我感到困惑的部分是弄清楚如何为每一列打分 - >将分号分隔的字符串转换为具有数值的列。 如果我能通过这个,我就可以创建所需的输出数据框。

我找到了 pmr 包,但文档只有少数。而且,那个包裹太超前了。我真的不需要当前状态,只需要每个偏好的简单分数

请在评分阶段帮助我

【问题讨论】:

    标签: r ranking


    【解决方案1】:

    这是一种使用一些 lapply()vapply() 调用的方法,但会推广到更多列。

    library(tibble)
    d <- tibble::tribble(
      ~Fruits,                          ~Color,
         "orange;apple;banana;avocado",     "blue;yellow;red;green",
         "avocado;apple;banana;orange",     "red;green;blue;yellow",
         "apple;banana;orange;avocado",     "yellow;red;green;blue",
         "banana;orange;apple;avocado",     "green;blue;red;yellow",
         "apple;avocado;banana;orange",     "yellow;blue;green;red"
    )
    
    
    x <- lapply(unname(d), (col) {
      l <- col %>% strsplit(";")
      x <- l[[1]] %>% unique() %>% sort()
      out <- lapply(x, (x) {
        vapply(l, FUN.VALUE = numeric(1), (vec) which(rev(vec) == x))
      })
      names(out) <- x
      as.data.frame(out)
    })
    
    do.call(cbind, x)
    
      apple avocado banana orange blue green red yellow
    1     3       1      2      4    4     1   2      3
    2     3       4      2      1    2     3   4      1
    3     4       1      3      2    1     2   3      4
    4     2       1      4      3    3     4   2      1
    5     4       3      2      1    3     2   1      4
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多