From 20cc89d922e08693acbc68cdd123866f20d5f7f8 Mon Sep 17 00:00:00 2001 From: Zihao Li Date: Tue, 17 Sep 2024 14:32:13 +0100 Subject: [PATCH] fix age issue in multiple years --- R/read_slf.R | 10 ++++++---- tests/testthat/test-multiple_years.R | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/R/read_slf.R b/R/read_slf.R index 622d9b8..ad9a167 100644 --- a/R/read_slf.R +++ b/R/read_slf.R @@ -62,6 +62,7 @@ read_slf <- function( # Check if recid/hscp is among the selected columns recid_present <- "recid" %in% selected_columns hscp_present <- "hscp2018" %in% selected_columns + age_present <- "age" %in% selected_columns # check if we need add extra recid/hscp to do filter # remember to remove recid/hscp later @@ -109,6 +110,11 @@ read_slf <- function( slf_table <- slf_table %>% dplyr::select(-c("hscp2018")) } + if (age_present){ + slf_table <- slf_table %>% + dplyr::mutate(age = as.integer(.data$age)) + } + return(slf_table) } ) @@ -184,10 +190,6 @@ read_slf_episode <- function( data <- data %>% dplyr::mutate(keytime2 = hms::as_hms(.data$keytime2)) } - if ("age" %in% names(data)) { - data <- data %>% - dplyr::mutate(age = as.integer(age)) - } return(data) } diff --git a/tests/testthat/test-multiple_years.R b/tests/testthat/test-multiple_years.R index 842d37f..7d7a0a8 100644 --- a/tests/testthat/test-multiple_years.R +++ b/tests/testthat/test-multiple_years.R @@ -6,7 +6,7 @@ test_that("read multiple years works for individual file", { # Read in multiple years indiv <- read_slf_individual(c("1718", "1819"), - col_select = c("year", "anon_chi") + col_select = c("year", "anon_chi", "age") ) %>% dplyr::group_by(year) %>% dplyr::slice_sample(n = 50) %>%