Skip to contents
  • collapse_or_na() joins the non-missing, non-empty values of a vector into one string, and returns NA when there is none (rather than an empty string, which a database would store as a value).

  • json_list_to_string() does the same for JSON arrays stored as text ('["Economics","History"]'), as some APIs return them. It needs the jsonlite package.

Usage

collapse_or_na(x, sep = "; ")

json_list_to_string(x, sep = "; ")

Arguments

x

A vector or a list (collapse_or_na()), or a character vector of JSON arrays (json_list_to_string()).

sep

Character. Separator.

Value

collapse_or_na(): a string. json_list_to_string(): a character vector of the same length as x.

Examples

collapse_or_na(c("Economics", NA, "", "History"))
#> [1] "Economics; History"
collapse_or_na(c(NA, NA))
#> [1] NA
json_list_to_string(c('["Economics","History"]', "NULL", "not json"))
#> [1] "Economics; History" NA                   NA