count_if (EXPSS) 在 R 中有多个条件
count_if (EXPSS) with multiple conditions in R
我正在使用 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
我正在使用 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