rdataframecountexpss

count_if (EXPSS) with multiple conditions in R


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.


Solution

  • 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