1

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)
Lynda
  • 141
  • 7

1 Answers1

1

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))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    Thanks so much @akrun! This works brilliantly! I am only a beginner of R so would this work with a matrix of strings too or would I need to use another function in place of sum such as count? – Lynda Oct 11 '19 at 17:24
  • @Lynda If your strings are `"0"`, it would match that, but if it is `"00"`, it needs to specify exactly as `==` is an exact matching operator and not a regex `"00" == 0`. Also, pleasee check for leading/lagging spaces `" 0"` or `"0 "`. In that cases, you need `trimws` – akrun Oct 11 '19 at 17:26
  • 1
    Ok great @akrun thanks for explaining! This makes sense to me now! – Lynda Oct 11 '19 at 17:27