3

I am using expss::count_if.

While something like this works fine (i.e., counting values only where value is equal to "1"):

(number_unemployed = count_if("1",unemployed_field,na.rm = TRUE)),

This does not (i.e., counting values only where value is equal to "1" or "2" or "3"):

(number_unemployed = count_if("1", "2", "3", unemployed_field,na.rm = TRUE)),

What is the correct syntax for using multiple conditions for count_if? I cannot find anything in the expss package documentation.

M--
  • 25,431
  • 8
  • 61
  • 93
DiamondJoe12
  • 1,879
  • 7
  • 33
  • 81

1 Answers1

3

You need to put them into a vector. This works:

(number_unemployed = count_if(c("1", "2", "3"), unemployed_field), na.rm=T),

Example: Sample data is provided below;

library(expss)

count_if(c("1","2","3"),dt$Encounter) 
 #> 9

Data:

dt <- structure(list(Location = c("A", "B", "A", "A", "C", "B", "A", "B", "A", "A", "A"), 
                     Encounter = c("1", "2", "3", "1", "2", "3", "4", "1", "2", "3", "4")), 
                row.names = c(NA, -11L), class = "data.frame")

#    Location Encounter
# 1         A         1
# 2         B         2
# 3         A         3
# 4         A         1
# 5         C         2
# 6         B         3
# 7         A         4
# 8         B         1
# 9         A         2
# 10        A         3
# 11        A         4
M--
  • 25,431
  • 8
  • 61
  • 93
  • 1
    There is no argument `na.rm` in the `count_if`. In some cases presence of expression `na.rm = TRUE` can lead to incorrect results. – Gregory Demin Nov 21 '19 at 16:07
  • @GregoryDemin While I appreciate your edits, I should mention that you should deal your editing privilege with more caution. In short, give OP time to consider your point, and finally it's their decision to apply it or not. Your attempted edit can be seen as an attempt to answer which is not a legit edit. Your comment laid out your point. In the meantime, I appreciate if you could point me to the examples that `na.rm` would result in incorrect answers. Cheers. – M-- Nov 21 '19 at 16:11
  • 2
    Here is such an example: `count_if(T, c(T, F, F), na.rm = T)`. Since there is no `na.rm` argument, the value of `na.rm` is seen as just another element from `...` to count and so the output of the code is `2`, 1 from the second argument, and 1 from the third. – IceCreamToucan Nov 21 '19 at 16:14
  • 2
    Sorry for my hurry with editing. Example: `a = 1:2; count_if(1, a, na.rm = TRUE)` which gives 3 because TRUE in na.rm will be recycled and coerced to 1. – Gregory Demin Nov 21 '19 at 16:17
  • 1
    @IceCreamToucan makes sense. `...` gets number of vectors and counts the elements that are meeting the `criterion`. Cheers. – M-- Nov 21 '19 at 16:18
  • hmm, now I'm curious why the arguments are recycled in Gregory's example but not mine. `?count_if` says they should be recycled but that doesn't seem to be the case in `count_if(T, c(T, F, F), na.rm = T)`. If I do `count_if(1, c(1, 0, 0), na.rm = 1)` the output is `4`. Very odd... – IceCreamToucan Nov 21 '19 at 16:22
  • @IceCreamToucan It seems you found a bug. – Gregory Demin Nov 21 '19 at 16:32