Finding the number of 0s in a number of binary matrices in the same directory?

51 Views Asked by At

I am trying to get the number of 0s (or count of) in a number of binary matrix csv files? Is there an efficient way of doing this for multiple csv files at once?

I have read in all the csv files from the set working directory using this code but I am unsure where to go from here...

matrices <- list.files(pattern="*.txt")
matrices <- lapply(matrices, read.delim)
1

There are 1 best solutions below

3
On BEST ANSWER

An option is to create a logical matrix (x == 0) by looping through the list of data.frames (sapply) and use sum. In case, there are NA values, use the na.rm = TRUE (by default it is FALSE)

sapply(files, function(x) sum(x == 0, na.rm = TRUE))