Reputation: 606
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
[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")
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
Upvotes: 8
Views: 4114
Reputation: 18610
assuming that TiposMotivA and ModaLinhaA have the same length (which I guess is the case in your full dataset):
data.frame(Row = 1:nrow(TiposMotivA),
Mode = ModaLinhaA,
Qt = rowSums(TiposMotivA == rep(ModaLinhaA,ncol(TiposMotivA))))
Upvotes: 5
Reputation: 3290
You can write a simple function to count the most common number in a vector and then apply it to each row with apply()
.
Note: I used @Ken_William 's awesome function for determining the mode of a vector in the below code.
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.
Upvotes: 3