R计算值在每行中出现的次数

时间:2022-11-29 15:43:25

I need to find out for each mode (mode for a row, not a column) value obtainned, how many times it appear in its row from my data.

我需要找出每个模式(行的模式,而不是列)获得的值,它在我的数据行中出现的次数。

This is my data

这是我的数据

> head(TiposMotivA)
  Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12 Q13 Q14 Q15 Q16 Q17 Q18 Q19 Q20 Q21
1  5  4  4  4  6  6  7  6  4   6   6   6   4   4   4   4   6   7   4   4   6
2  5  4  4  5  5  5  5  5  5   5   7   5   4   3   1   6   6   5   6   7   7
3  4  5  4  4  5  4  5  4  5   4   5   4   5   4   5   4   5   4   5   4   5
4  5  5  7  7  4  6  6  6  7   7   6   7   7   6   6   7   4   7   6   6   7
5  6  1  7  6  7  7  7  7  7   7   6   7   2   2   3   6   3   7   7   7   7
6  4  4  3  3  4  5  4  3  4   7   6   6   4   4   6   4   5   7   6   6   7

Dput from this dataset

从此数据集输入

dput(head(TiposMotivA))
    structure(list(Q1 = c(5L, 5L, 4L, 5L, 6L, 4L), Q2 = c(4L, 4L, 
    5L, 5L, 1L, 4L), Q3 = c(4L, 4L, 4L, 7L, 7L, 3L), Q4 = c(4L, 5L, 
    4L, 7L, 6L, 3L), Q5 = c(6L, 5L, 5L, 4L, 7L, 4L), Q6 = c(6L, 5L, 
    4L, 6L, 7L, 5L), Q7 = c(7L, 5L, 5L, 6L, 7L, 4L), Q8 = c(6L, 5L, 
    4L, 6L, 7L, 3L), Q9 = c(4L, 5L, 5L, 7L, 7L, 4L), Q10 = c(6L, 
    5L, 4L, 7L, 7L, 7L), Q11 = c(6L, 7L, 5L, 6L, 6L, 6L), Q12 = c(6L, 
    5L, 4L, 7L, 7L, 6L), Q13 = c(4L, 4L, 5L, 7L, 2L, 4L), Q14 = c(4L, 
    3L, 4L, 6L, 2L, 4L), Q15 = c(4L, 1L, 5L, 6L, 3L, 6L), Q16 = c(4L, 
    6L, 4L, 7L, 6L, 4L), Q17 = c(6L, 6L, 5L, 4L, 3L, 5L), Q18 = c(7L, 
    5L, 4L, 7L, 7L, 7L), Q19 = c(4L, 6L, 5L, 6L, 7L, 6L), Q20 = c(4L, 
    7L, 4L, 6L, 7L, 6L), Q21 = c(6L, 7L, 5L, 7L, 7L, 7L)), .Names = c("Q1", 
    "Q2", "Q3", "Q4", "Q5", "Q6", "Q7", "Q8", "Q9", "Q10", "Q11", 
    "Q12", "Q13", "Q14", "Q15", "Q16", "Q17", "Q18", "Q19", "Q20", 
    "Q21"), row.names = c(NA, 6L), class = "data.frame")

Those are the modes for each ROW

这些是每个ROW的模式

    [1] "4" "5" "4"   "7"   "7"   "4"   "7"   "6"   "7"   "7"   "7"   "7"   "7" 
    [14] "5" "7" "6"   "7"   "6"   "7"   "7"   "7"   "7"   "7"   "7"   "7"   "7"
    [27] "7" "7" "7"   "5"   "2"   "7"   "7"   "7"   "7"   "7"   "6"   "6"   "7"
    [40] "4" "3"   "4"   "7"   "5" "6"   "7"   "7"   "6"   "7"   "6"   "7"   "7"
    [53] "7"   "6"   "7"   "7"   "5" "7"   "7"   "7"   "7"   "7" 


> 

Dput for this one

输入这个

dput(ModaLinhaA) c("4", "5", "4", "7", "7", "4", "7", "6", "7", "7", "7", "7", "7", "5", "7", "6", "7", "6", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "5", "2", "7", "7", "7", "7", "7", "6", "6", "7", "4", "3", "4", "7", "5", "6", "7", "7", "6", "7", "6", "7", "7", "7", "6", "7", "7", "5", "7", "7", "7", "7", "7")

dput(ModaLinhaA)c(“4”,“5”,“4”,“7”,“7”,“4”,“7”,“6”,“7”,“7”,“7”, “7”,“7”,“5”,“7”,“6”,“7”,“6”,“7”,“7”,“7”,“7”,“7”,“7 “,”7“,”7“,”7“,”7“,”7“,”5“,”2“,”7“,”7“,”7“,”7“,”7“, “6”,“6”,“7”,“4”,“3”,“4”,“7”,“5”,“6”,“7”,“7”,“6”,“7 “,”“6”,“7”,“7”,“7”,“6”,“7”,“7”,“5”,“7”,“7”,“7”,“7”, “7”)

Now I need to count how many times each mode appears in each row. The response should be something like this:

现在我需要计算每个模式在每一行中出现的次数。响应应该是这样的:

Row    Mode    Qt
  1       4    10
  2       5    10
  3       4    11

2 个解决方案

#1


5  

assuming that TiposMotivA and ModaLinhaA have the same length (which I guess is the case in your full dataset):

假设TiposMotivA和ModaLinhaA具有相同的长度(我猜你的完整数据集就是这种情况):

data.frame(Row = 1:nrow(TiposMotivA),
           Mode = ModaLinhaA,
           Qt = rowSums(TiposMotivA == rep(ModaLinhaA,ncol(TiposMotivA))))

#2


3  

You can write a simple function to count the most common number in a vector and then apply it to each row with apply().

您可以编写一个简单的函数来计算向量中最常用的数字,然后使用apply()将其应用于每一行。

Note: I used @Ken_William 's awesome function for determining the mode of a vector in the below code.

注意:我使用@Ken_William的强大功能来确定下面代码中的向量模式。

Mode <- function(x) { # @Ken_Williams's formula for mode
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

TiposMotivA$Qt <- apply(TiposMotivA, 1, function(x) sum(x == Mode(x)))

sum(x == Mode(x)) is taking the sum of the logical vector returned by x == Mode(x). TRUE values count as 1 and FALSE values count as zero so the sum of the vector will be the count the modal entry.

sum(x == Mode(x))取x == Mode(x)返回的逻辑向量的总和。 TRUE值计为1,FALSE值计为0,因此向量的总和将是模态条​​目的计数。

#1


5  

assuming that TiposMotivA and ModaLinhaA have the same length (which I guess is the case in your full dataset):

假设TiposMotivA和ModaLinhaA具有相同的长度(我猜你的完整数据集就是这种情况):

data.frame(Row = 1:nrow(TiposMotivA),
           Mode = ModaLinhaA,
           Qt = rowSums(TiposMotivA == rep(ModaLinhaA,ncol(TiposMotivA))))

#2


3  

You can write a simple function to count the most common number in a vector and then apply it to each row with apply().

您可以编写一个简单的函数来计算向量中最常用的数字,然后使用apply()将其应用于每一行。

Note: I used @Ken_William 's awesome function for determining the mode of a vector in the below code.

注意:我使用@Ken_William的强大功能来确定下面代码中的向量模式。

Mode <- function(x) { # @Ken_Williams's formula for mode
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

TiposMotivA$Qt <- apply(TiposMotivA, 1, function(x) sum(x == Mode(x)))

sum(x == Mode(x)) is taking the sum of the logical vector returned by x == Mode(x). TRUE values count as 1 and FALSE values count as zero so the sum of the vector will be the count the modal entry.

sum(x == Mode(x))取x == Mode(x)返回的逻辑向量的总和。 TRUE值计为1,FALSE值计为0,因此向量的总和将是模态条​​目的计数。