Skip to content

Commit 338af6c

Browse files
committed
change is_selected to yield NA for NA response; update tests
1 parent 3a51172 commit 338af6c

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

facebook/delphiFacebook/R/variables.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ is_selected <- function(vec, selection) {
2727
selections <- sapply(
2828
vec,
2929
function(resp) {
30-
if (length(resp) == 0) {
30+
if (length(resp) == 0 || all(is.na(resp))) {
3131
# All our selection items include "None of the above" or similar, so
3232
# treat no selection the same as missingness.
3333
NA

facebook/delphiFacebook/tests/testthat/test-variables.R

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,32 @@ test_that("is_selected handles selections correctly", {
1212
expect_equal(is_selected(split_options(c("", "14", "4", "4,6,8")), "1"),
1313
c(NA, FALSE, FALSE, FALSE))
1414

15-
expect_equal(is_selected(split_options(c("1", "15", "14", NA)), "14"),
16-
c(FALSE, FALSE, TRUE, FALSE))
15+
expect_equal(is_selected(split_options(c("1", "15", "14", NA, "")), "14"),
16+
c(FALSE, FALSE, TRUE, NA, NA))
1717

1818
expect_equal(is_selected(split_options(c("4,54", "3,6,2,54", "5,4,45")),
1919
"54"),
2020
c(TRUE, TRUE, FALSE))
2121
})
2222

23+
test_that("activities items correctly coded", {
24+
input_data <- data.frame(
25+
C13 = c(NA, "1,2,4", "3", "", "6", "2,4")
26+
)
27+
28+
out <- code_activities(input_data)
29+
30+
# expected result
31+
input_data$a_work_outside_home_1d <- c(NA, TRUE, FALSE, NA, FALSE, FALSE)
32+
input_data$a_shop_1d <- c(NA, TRUE, FALSE, NA, FALSE, TRUE)
33+
input_data$a_restaurant_1d <- c(NA, FALSE, TRUE, NA, FALSE, FALSE)
34+
input_data$a_spent_time_1d <- c(NA, TRUE, FALSE, NA, FALSE, TRUE)
35+
input_data$a_large_event_1d <- c(NA, FALSE, FALSE, NA, FALSE, FALSE)
36+
input_data$a_public_transit_1d <- c(NA, FALSE, FALSE, NA, TRUE, FALSE)
37+
38+
expect_equal(out, input_data)
39+
})
40+
2341
test_that("mask items correctly coded", {
2442
input_data <- data.frame(
2543
C14 = c(NA, 1, 3, 6, 2, 4),

0 commit comments

Comments
 (0)