[英]R count number of times a value appears in each row
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
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))))
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)
. sum(x == Mode(x))
取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. TRUE
值计为1, FALSE
值计为0,因此向量的总和将是模态条目的计数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.