Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R count number of times a value appears in each row

Tags:

r

mode

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
like image 673
Spartacus Rocha Avatar asked Jan 29 '15 04:01

Spartacus Rocha


People also ask

How do you count the number of times a value appears in R?

To count the number of times a value occurs in a column of an R data frame, we can use table function for that particular column.

How do I count the number of occurrences of a string in R?

The stringr package provides a str_count() method which is used to count the number of occurrences of a certain pattern specified as an argument to the function. The pattern may be a single character or a group of characters. Any instances matching to the expression result in the increment of the count.

How do you count the number of times a value appears in a column?

Count how often a single value occurs by using the COUNTIF function. Use the COUNTIF function to count how many times a particular value appears in a range of cells.


2 Answers

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))))
like image 72
RockScience Avatar answered Oct 11 '22 15:10

RockScience


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.

like image 39
Richard Border Avatar answered Oct 11 '22 15:10

Richard Border