Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the number of specific integers per column in an R matrix

I have a matrix (10 x 100) where I need to count the number of each integer per column so I have a final matrix that is (3 x 100). Counts for 0, 1, and 2 per column.

I think the apply function will be useful here, the code I provided is a solution I envision.

Any help will be greatly appreciated.

library(dplyr)
set.seed(100)
a <- matrix(sample(0:2, size=100, replace=TRUE), nrow=10, ncol=100)
out <- apply(a, 2, function(x) count(x))

 Desired output: rows are the sum of each variable "0, 1, 2"

   1 2 3 ...  n
 0 1 1 3
 1 6 3 3
 2 3 6 4
like image 906
Connor Murray Avatar asked Oct 29 '19 12:10

Connor Murray


People also ask

How to count the number of occurrences of certain values in R?

You can use the following syntax in R to count the number of occurrences of certain values in columns of a data frame: The following examples show how to use this syntax in practice with the following data frame: The following code shows how to count the number of occurrences of each value in the ‘team’ column: The team name ‘Mavs’ appears 2 times.

How to count the number of Na's in a column in R?

The easiest way to count the number of NA’s in R in a single column is by using the functions sum () and is.na (). The is.na () function takes one column as input and converts all the missing values into ones and all other values into zeros. Then, using the sum () function, one can sum all the ones and thus count the number of NA’s in a column.

How to count the number of Na’s in Excel?

One kind of counting the number of NA’s is row-wise. That is to say, to count the frequency of the missing values per row. On the contrary, you can also count the number of NA’s per column (i.e., column-wise).


1 Answers

There is a function called table that counts the distinct values of the whole object. You can apply it to each column, i.e.

apply(a, 2, table)

to include NAs in the count just use the option useNA, i.e.

apply(a, 2, table, useNA = 'always')

#or with complete syntax
apply(a, 2, function(i)table(i, useNA = 'always'))

As @IceCreamToucan mentions in comments, If you have missing values in any column, then you want be able to coerce to to a data frame (or matrix for that matter). To overcome this, we can convert each column to factor with levels = c(0:2), i.e.

apply(a, 2, function(i) table(factor(i, levels = c(0:2)), useNA = 'always'))
like image 175
Sotos Avatar answered Sep 22 '22 06:09

Sotos