Skip to content

Commit

Permalink
Replace NA with "" in character columns
Browse files Browse the repository at this point in the history
  • Loading branch information
hsonne committed Nov 28, 2023
1 parent e20aaa0 commit 2396d3a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
14 changes: 11 additions & 3 deletions R/readAndMergeEuCodedFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,18 @@ mergeInspectionData <- function(x, warn = FALSE)
obs
})

header.info <- get_elements(x[[1L]], "header.info")
inspections <- kwb.utils::safeRowBindOfListElements(x, "inspections")
observations <- kwb.utils::safeRowBindAll(observations)

# Replace NA with "" in columns of type character
inspections <- replaceNaWithEmptyStringInCharColumns(inspections)
observations <- replaceNaWithEmptyStringInCharColumns(observations)

list(
header.info = get_elements(x[[1L]], "header.info"),
inspections = kwb.utils::safeRowBindOfListElements(x, "inspections"),
observations = kwb.utils::safeRowBindAll(observations)
header.info = header.info,
inspections = inspections,
observations = observations
)
}

Expand Down
13 changes: 13 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ removeEmptyLines <- function(x, dbg = TRUE)
)
}

# replaceNaWithEmptyStringInCharColumns ----------------------------------------
replaceNaWithEmptyStringInCharColumns <- function(x)
{
is_char <- sapply(x, is.character)

x[is_char] <- lapply(x[is_char], function(y) {
y[is.na(y)] <- ""
y
})

x
}

# valuesToCsv ------------------------------------------------------------------

#' Values to CSV
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#library(testthat)
test_that("replaceNaWithEmptyStringInCharColumns() works", {

f <- kwb.en13508.2:::replaceNaWithEmptyStringInCharColumns

expect_error(f())

x <- data.frame(
a = 1:2,
b = c("x", NA)
)

expect_identical(f(x)[["b"]], c("x", ""))

})

0 comments on commit 2396d3a

Please sign in to comment.