R counts the number of times a value appears in each row

I need to find out for each mode value received (for a row, not a column) how many times it appears 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") 

These 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

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 on each line. The answer should be something like this:

 Row Mode Qt 1 4 10 2 5 10 3 4 11 
+7
r mode
source share
2 answers

assuming TiposMotivA and ModaLinhaA are the same length (which, I think, takes place in your complete dataset):

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

You can write a simple function to calculate the most common number in a vector, and then apply it to each line using apply() .

Note. I used @Ken_William amazing function to determine the vector mode in the code below.

 Mode <- function(x) { # @Ken_Williams 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)) takes the sum of the logical vector returned by x == Mode(x) . TRUE values ​​are considered as 1 and FALSE values ​​are considered as zero, so the sum of the vector will be considered as a modal record.

+3
source share

All Articles