我正在使用expss::count_if

虽然类似的方法可以正常工作(即,仅在值等于“1”的地方对值进行计数):

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

这不是(即仅在值等于“1”,“2”或“3”的情况下对值进行计数):
(number_unemployed = count_if("1", "2", "3", unemployed_field,na.rm = TRUE)),

count_if使用多个条件的正确语法是什么?我在expss软件包文档中找不到任何内容。

最佳答案

您需要将它们放入向量中。这有效:

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

示例:下面提供了示例数据;

library(expss)

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

数据:
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

10-04 14:29