Skip to content

Commit

Permalink
functions to convert empty strings to NA and vice versa
Browse files Browse the repository at this point in the history
  • Loading branch information
polettif committed Jan 27, 2022
1 parent 2593862 commit 4d3962e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ S3method(plot,tidygtfs)
S3method(print,tidygtfs)
S3method(summary,tidygtfs)
export(cluster_stops)
export(empty_strings_to_na)
export(filter_feed_by_area)
export(filter_feed_by_date)
export(filter_feed_by_stops)
Expand Down
3 changes: 3 additions & 0 deletions R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ write_gtfs <- function(gtfs_obj, zipfile, compression_level = 9, as_dir = FALSE)
# convert sf tables
gtfs_out = sf_as_tbl(gtfs_obj)

# convert NA to empty strings
gtfs_out <- na_to_empty_strings(gtfs_out)

# data.tables
gtfs_out <- gtfs_out[names(gtfs_out) != "."]
gtfs_out <- lapply(gtfs_out, as.data.table)
Expand Down
39 changes: 39 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,42 @@ feed_contains <- function(gtfs_obj, table_name) {
exists(table_name, where = gtfs_obj) ||
(exists(".", where = gtfs_obj) && exists(table_name, where = gtfs_obj$.))
}

#' Convert empty strings ("") to NA values in gtfs tables
#'
#' @param gtfs_obj tidygtfs object
#'
#' @return a gtfs_obj where all empty strings in tables have been replaced with NA
#'
#' @export
empty_strings_to_na = function(gtfs_obj) {
tbl_names = names(gtfs_obj)
tbl_names <- tbl_names[tbl_names != "."]
for(tbl in tbl_names) {
if(inherits(gtfs_obj[[tbl]], "data.frame")) {
gtfs_obj[[tbl]][gtfs_obj[[tbl]] == ""] <- NA
}
}
gtfs_obj
}

#' Convert NA values to empty strings ("")
#'
#' @param gtfs_obj tidygtfs object
na_to_empty_strings = function(gtfs_obj) {
lapply(gtfs_obj, function(df) {
if(inherits(df, "data.frame")) {
df2 = lapply(df, function(.col) {
if(is.character(.col)) {
.col[is.na(.col)] <- ""
}
.col
})
attributes(df2) <- attributes(df)
df <- df2
} else if(inherits(df, "list")) {
df <- na_to_empty_strings(df)
}
df
})
}
17 changes: 17 additions & 0 deletions man/empty_strings_to_na.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/na_to_empty_strings.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,21 @@ test_that("gtfs_meta", { # empty test
expect_equal(gtfs_meta, get_gtfs_meta())
})

test_that("empty_strings_to_na", {
gpath = system.file("extdata", "sample-feed-fixed.zip", package = "tidytransit")
g1 = read_gtfs(gpath)
g_na = empty_strings_to_na(g1)
g2 = na_to_empty_strings(g_na)

for(tbl in names(g1)) {
expect_equal(g1[[tbl]], g2[[tbl]])
}

tmppath = tempfile(fileext = ".zip")
write_gtfs(g_na, tmppath)
g3 = read_gtfs(tmppath)

for(tbl in names(g1)) {
expect_equal(g1[[tbl]], g3[[tbl]])
}
})

0 comments on commit 4d3962e

Please sign in to comment.