-1

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)
Jordan
  • 67
  • 2
  • 7
  • Must the 1's need to be consecutive? – Rui Barradas Oct 12 '19 at 10:59
  • 2
    Hi Jordan, it will help to have reproducible example, see : https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. I would also recommend focusing on the specific problem, either reading data from files, or manipulating data in a given example – Bulat Oct 12 '19 at 11:01

1 Answers1

0

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))
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • The 1s are not consecutive so this works - thanks so much @RuiBarradas. Can you please explain what set.seed and rbinom does? I have never came across these two functions before. – Jordan Oct 12 '19 at 11:32
  • @Jordan `rbinom` is the function to generate pseudo random binomial numbers. So, to make the result reproducible, I set the RNG seed before calling it. I will edit the answer to explain that that is meant to have a working data set example. – Rui Barradas Oct 12 '19 at 11:47