3

我正在使用expss::count_if.

虽然这样的工作正常(即,仅在值等于“1”时计算值):

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

这不会(即,仅在 value 等于“1”或“2”或“3”时计算值):

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

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

4

1 回答 1

3

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

(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
于 2019-11-21T16:03:47.600 回答