I am looking for a way to find the number of rows consisting of three or more 1s in a number of binary matrices stored in individual .csv files in the same working directory. Is there a way in R to do such a thing? I have read in all the files in the set working directory with the following but I am unsure what to do next...
file_list <- list.files(pattern="*.csv")
files <- lapply(file_list, read.delim)
If the 1's do not need to be consecutive, here is a way of counting the rows with 3 1's.
Firs create a matrix of random 0's and 1's, to test the code. Then, define a function, rowOnes
, that does the real work and run it on the example matrix mat
.
set.seed(1234)
mat <- matrix(rbinom(24, 1, 0.5), nrow = 4)
rowOnes <- function(X, na.rm = FALSE) rowSums(X == 1, na.rm = na.rm)
rowOnes(mat)
#[1] 2 4 2 3
sum(rowOnes(mat) >= 3)
#[1] 2
Now *apply
the strategy above to the list of matrices.
sapply(files, function(x) sum(rowOnes(x) >= 3))