-6

I'm trying to count the # of 0's in each column in my zoo [...dataframe?], similar to how I count the # of na's.

dropColumns = sapply(test1_z, function(x) sum(is.na(x)))

I've tried

View(sapply(test1_z, function(x) count(x[x==0])))

but count doesn't like zoo objects (test1_z). Length works here, but not count.

Any ideas would be greatly appreciated

My professor recommended

sum(frd$col0 == 0)

but that only works for a single column. I was hoping to do this over an entire dataframe.

thistleknot
  • 1,098
  • 16
  • 38
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Dec 10 '18 at 21:29
  • easier yes to troubleshoot, not necessarily easy to implement for a simple question. Hopefully I don't have to export a bunch of data to get a simple answer – thistleknot Dec 10 '18 at 21:30
  • 1
    `zoo` objects and `data.frames` are very different things. This is why a reproducible example would make it clear what exactly you are working with so possible solutions can be verified to work in your specific case. – MrFlick Dec 10 '18 at 21:45
  • I'm sure it would, but let's see what's faster – thistleknot Dec 10 '18 at 21:47

1 Answers1

2

Create a test zoo object z and then use the indicated expression. If you know there are no NAs then the na.rm = TRUE argument can be omitted. This also works for a data frame and also for a matrix.

library(zoo)

# test data
z <- zoo(BOD)
z[2:3, 1] <- 0
z[4, 2] <- 0
z[5, 1] <- NA

colSums(z == 0, na.rm = TRUE)

giving:

  Time demand 
     2      1 
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • hrmm, unfortunately for me my hopes for a quick answer are going to evade me. That produces table/tabular result for sure, but every value is na (except for date due to my zoo array having na's? not sure). – thistleknot Dec 10 '18 at 21:49
  • doing a apply(your.matrix, 2, function(c)sum(c!=0)) results in na's as well except for date, where there is 123... I suspect the function get's confused with 0's and na's in the mix – thistleknot Dec 10 '18 at 21:51
  • have added na.rm=TRUE – G. Grothendieck Dec 10 '18 at 22:06
  • omg, you are my hero! – thistleknot Dec 10 '18 at 22:24
  • thank you for seeing the problem and trying to help without layering the issue with bureaucratic red tape – thistleknot Dec 10 '18 at 22:29
  • 2
    Nevertheless the rules are there for a reason. Questions which do not include minimal reproducible examples tend to be ambiguous and generate a lot of needless distracting discussion about what the question is and the different answers tend to differ more on their interpretation of the problem rather than focusing on the answer. – G. Grothendieck Dec 10 '18 at 22:32
  • having the data ready is a plus, but in my case it would be more work than it's worth as my data sets are quite large, I'd have to construct a dummy data set for what I was hoping was a simple answer, and in this case it was NA=TRUE. there is a trade off. I think you and I found the nice balance and I appreciate you attempting the effort before red taping me – thistleknot Dec 10 '18 at 23:42
  • 1
    Questions are supposed to provide a minimal example, not the original data. By not including an example extra time was taken due to the NA issue and that could have been avoided. Also there was a question about whether you really wanted zoo objects, matrix objects and data.frame objects. – G. Grothendieck Dec 10 '18 at 23:46