Skip to content

Commit 0cf2ef1

Browse files
authored
Merge pull request #844 from cmu-delphi/fb-package-is-selected-missing
[fb survey] `is_selected` to map missing responses to `NA`
2 parents 3b1033b + db91195 commit 0cf2ef1

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

facebook/delphiFacebook/R/variables.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ split_options <- function(column) {
1919

2020
#' Test if a specific selection is selected
2121
#'
22+
#' Checking whether a specific selection is selected in either "" (empty
23+
#' string) or `NA` responses will produce `NA`s.
24+
#'
2225
#' @param vec A list whose entries are character vectors, such as c("14", "15").
2326
#' @param selection one string, such as "14"
2427
#' @return a logical vector; for each list entry, whether selection is contained
@@ -27,9 +30,11 @@ is_selected <- function(vec, selection) {
2730
selections <- sapply(
2831
vec,
2932
function(resp) {
30-
if (length(resp) == 0) {
31-
# All our selection items include "None of the above" or similar, so
32-
# treat no selection the same as missingness.
33+
if (length(resp) == 0 || all(is.na(resp))) {
34+
# Qualtrics files code no selection as "" (empty string), which is
35+
# parsed by `read_csv` as `NA` (missing) by default. Since all our
36+
# selection items include "None of the above" or similar, treat both no
37+
# selection ("") or missing (NA) as missing, for generality.
3338
NA
3439
} else {
3540
selection %in% resp

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)