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)
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))