简体   繁体   English

在R中的数据帧中找到最大的重复值量

[英]Finding highest amount of repeating value in data frame in R

Beginning R programmer here. 从这里开始R程序员。

I have a data frame called 'narc' that has recorded answers to 40 different questions measuring narcissism. 我有一个名为“ narc”的数据框,其中记录了测量自恋的40个不同问题的答案。

It looks like this: 看起来像这样:

 Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12 Q13 Q14 Q15 Q16
1723  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0
7231  2  2  2  1  1  2  1  1  2   2   2   2   2   2   1   2
5556  2  2  2  1  2  2  2  1  1   2   2   1   2   2   1   1
1511  2  2  2  2  2  2  2  2  2   2   2   2   2   2   2   2
2080  1  1  2  2  1  1  2  2  2   1   1   2   1   2   1   1
1074  2  2  1  1  2  2  2  1  1   1   1   1   2   2   1   2
     Q17 Q18 Q19 Q20 Q21 Q22 Q23 Q24 Q25 Q26 Q27 Q28 Q29 Q30
1723   0   0   0   0   0   0   0   0   0   0   0   0   0   0
7231   1   2   2   1   2   1   1   2   2   1   1   1   2   2
5556   1   1   1   1   1   2   1   2   2   1   2   1   2   1
1511   2   2   2   2   2   2   2   2   2   2   2   2   2   2
2080   1   1   1   1   2   1   2   1   1   1   2   1   1   1
1074   2   1   1   1   1   1   2   2   2   1   1   1   2   2
     Q31 Q32 Q33 Q34 Q35 Q36 Q37 Q38 Q39 Q40 elapse gender age
1723   0   0   0   0   0   0   0   0   0   0      8      1  23
7231   2   1   1   1   1   2   2   2   2   1     24      1  21
5556   2   1   1   2   1   1   2   2   2   1     33      2  18
1511   2   2   2   2   2   2   2   2   2   2     51      1  16
2080   2   2   1   1   2   1   1   2   2   2     59      1  20
1074   1   1   1   1   1   2   2   1   2   1     60      1  24
     score      level
1723     0        not
7231     8        not
5556    11        not
1511    17     mildly
2080    21 moderately
1074    14        not

I also have a data frame called 'narc.key' which has the answers that correspond to narcissism and it looks like this: 我还有一个名为“ narc.key”的数据框,其中包含与自恋相对应的答案,看起来像这样:

  Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12 Q13 Q14 Q15 Q16 Q17
1  1  1  1  2  2  1  2  1  2   2   1   1   1   1   2   1   2
  Q18 Q19 Q20 Q21 Q22 Q23 Q24 Q25 Q26 Q27 Q28 Q29 Q30 Q31 Q32
1   2   2   2   1   2   2   1   1   2   1   2   1   1   1   2
  Q33 Q34 Q35 Q36 Q37 Q38 Q39 Q40
1   1   1   2   1   1   1   1   2

I want to find out which question had the highest number of narcissistic answers. 我想找出哪个问题的自恋答案最多。

My approach to this problem would be to create a vector of values to record the number of rows that matched with narc.key for each column. 解决这个问题的方法是创建一个值向量,以记录与narc.key相匹配的每一行的行数。 However, I'm having some trouble as to how to do this. 但是,我在执行此操作时遇到了一些麻烦。 Here is my code so far: 到目前为止,这是我的代码:

  for (i in 1:nrow(narc)){

  }
    for(x in 1:40){
      highest.score<-narc[i]
      for(y in 1:40)
        if(narc[i,y]==narc.key[1,y]){

I'm having a hard time wrapping my head around what to do next. 我很难把头放在下一步该做什么。 Please help? 请帮忙?

This isn't fancy, but you can create an answer data set the same size as the questions and just use == (provided that your data sets are not really huge, this should not be too slow) 这不是花哨的,但是您可以创建一个与问题大小相同的答案数据集,并只使用== (前提是您的数据集不是真的很大,这应该不会太慢)

qs <- read.table(header = TRUE, text="Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12 Q13 Q14 Q15 Q16
  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0
  2  2  2  1  1  2  1  1  2   2   2   2   2   2   1   2
  2  2  2  1  2  2  2  1  1   2   2   1   2   2   1   1
  2  2  2  2  2  2  2  2  2   2   2   2   2   2   2   2
  1  1  2  2  1  1  2  2  2   1   1   2   1   2   1   1
  2  2  1  1  2  2  2  1  1   1   1   1   2   2   1   2")

ans <- read.table(header = TRUE, text="  Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12 Q13 Q14 Q15 Q16
1  1  1  1  2  2  1  2  1  2   2   1   1   1   1   2   1")


ans[1:nrow(qs), ] <- ans[1, ]

#   Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12 Q13 Q14 Q15 Q16
# 1  1  1  1  2  2  1  2  1  2   2   1   1   1   1   2   1
# 2  1  1  1  2  2  1  2  1  2   2   1   1   1   1   2   1
# 3  1  1  1  2  2  1  2  1  2   2   1   1   1   1   2   1
# 4  1  1  1  2  2  1  2  1  2   2   1   1   1   1   2   1
# 5  1  1  1  2  2  1  2  1  2   2   1   1   1   1   2   1
# 6  1  1  1  2  2  1  2  1  2   2   1   1   1   1   2   1

And then sum the columns: 然后对各列求和:

colSums(qs == ans)

# Q1  Q2  Q3  Q4  Q5  Q6  Q7  Q8  Q9 Q10 Q11 Q12 Q13 Q14 Q15 Q16 
# 1   1   1   2   3   1   4   3   3   3   2   2   1   0   1   2 

EDIT: I misread the need. 编辑:我误读了需要。 This answer indicates which questions had, as their most common response, a narc answer. 该答案表明哪些问题作为最常见的回答是narc答案。 NOT which question had the highest number of narc responses. 不是哪个问题的narc回答数量最多。

Here's one way. 这是一种方法。 The library is so I can use pipes to make it easier to follow. 该库是为了使我可以使用管道使其更容易理解。 I have a matrix with 40 columns, 10 rows - all randomly chosen to be 0, 1 or 2. I use the apply function, across columns, and tabulate the responses. 我有一个40列,10行的矩阵-全部随机选择为0、1或2。我在各列之间使用apply函数,并对响应列表。 Then, I apply to each of the elements of the list (there are 40, one per column) the function, which.max which will return the index of the highest value in the table. 然后,我将函数which.max列表中的每个元素(每列有40个元素), which.max将返回表中最大值的索引。 Then, I apply to each of these list elements (again, 40) and ask for the names of the of the value returned by which.max which will give me the response with the highest count. 然后,我将这些元素应用于每个列表元素(再次为40),并要求which.max返回的值的names ,这将使我获得最高计数的响应。 Finally, I unlist and turn them in to integers (otherwise they're text). 最后,我unlist并将其变成整数(否则它们是文本)。 This can be compared against the narc key to see which answers match. 可以将其与narc键进行比较,以查看匹配的答案。

library(magrittr)
mat = matrix(sample(c(0,1,2),400,replace=T),ncol=40)
output = mat %>%
  apply(2,table) %>%
  lapply(which.max) %>%
  lapply(names) %>%
  unlist() %>%
  as.integer()
narc.key == output

The one unintended outcome of this is that if there's a tie for most common, you may get back the first answer, not both. 这样做的一个意想不到的结果是,如果最常见的问题并列,您可能会得到第一个答案,而不是两者兼而有之。

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

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