1

给定一个排名问题的输出:

df <- structure(list(rank1 = c("A", "NA", "C", "B", "A"), rank2 = c("B", 
"NA", "A", "A", "B"), rank3 = c("C", "NA", "B", "C", "NA"), rank4 = c("D", 
"NA", "E", "D", "NA"), rank5 = c("E", "NA", "D", "E", "NA")), .Names = c("rank1", 
 "rank2", "rank3", "rank4", "rank5"), class = "data.frame", row.names = c("1", 
 "2", "3", "4", "5"))


  rank1 rank2 rank3 rank4 rank5
1     A     B     C     D     E
2    NA    NA    NA    NA    NA
3     C     A     B     E     D
4     B     A     C     D     E
5     A     B    NA    NA    NA

我想计算每个字母的排名分数 = 字母上每个排名的计数数 * 每个排名的权重(rank1 = 5 分,rank2 = 4 分,...,rank5 = 1 分)/总响应数.

为了按字母计算每个等级的计数,我使用了这个:

table(unlist(data1), c(col(data1)))

     1 2 3 4 5
  A  2 2 0 0 0
  B  1 2 1 0 0
  C  1 0 2 0 0
  D  0 0 0 2 1
  E  0 0 0 1 2
  NA 1 1 2 2 2

所以现在我想计算每个字母的分数(例如 A = (2 * 5 + 2 * 4) / 4)我不知道如何获得总响应数,然后如何创建一个函数计算排名分数。

4

1 回答 1

1

使用 apply 函数,您可以遍历表的每一行,然后计算分数

apply(table(unlist(df), c(col(df))), 1, FUN = function(row){
  return((row[1] * 5 + row[2] * 4 + row[3] * 3 + row[4] * 2 + row[5] * 1) / sum(row))
})

 A        B        C        D        E       NA 
4.500000 4.000000 3.666667 1.666667 1.333333 2.625000 

The fun part of the apply function passes the entire row to the function, then its just a case of multiply each corresponding element of the row by the weighting, then dividing by the total number of respondents for that row

于 2020-08-20T15:29:29.127 回答