Skip to contents

Fill delimited lines with NA values

Usage

fillLines(object, format = c("csv", "tsv"))

Arguments

object

character.

format

character(1). Delimited input format.

Value

character. Modified character vector, with consistent number of delimited values.

Note

Updated 2023-09-27.

See also

  • textConnection: For import approach after modification.

Examples

## character ====
## CSV format.
object <- c(
    "\"aaa\",\"bbb\",\"ccc\",\"ddd\"",
    "\"eee\",\"fff\",\"ggg\"",
    "\"hhh\",\"iii\""
)
print(object)
#> [1] "\"aaa\",\"bbb\",\"ccc\",\"ddd\"" "\"eee\",\"fff\",\"ggg\""        
#> [3] "\"hhh\",\"iii\""                
object <- fillLines(object, format = "csv")
print(object)
#> [1] "\"aaa\",\"bbb\",\"ccc\",\"ddd\"" "\"eee\",\"fff\",\"ggg\",NA"     
#> [3] "\"hhh\",\"iii\",NA,NA"          
con <- textConnection(object)
object <- import(con, format = "csv")
#> → Importing text connection with base::`read.table()`.
print(object)
#>   aaa bbb  ccc ddd
#> 1 eee fff  ggg  NA
#> 2 hhh iii <NA>  NA
close(con)

## TSV format.
object <- c(
    "aaa\tbbb\tccc\tddd",
    "eee\tfff\tggg",
    "hhh\tiii"
)
print(object)
#> [1] "aaa\tbbb\tccc\tddd" "eee\tfff\tggg"      "hhh\tiii"          
object <- fillLines(object, format = "tsv")
print(object)
#> [1] "aaa\tbbb\tccc\tddd" "eee\tfff\tggg\tNA"  "hhh\tiii\tNA\tNA"  
con <- textConnection(object)
object <- import(con, format = "tsv")
#> → Importing text connection with base::`read.table()`.
print(object)
#>   aaa bbb  ccc ddd
#> 1 eee fff  ggg  NA
#> 2 hhh iii <NA>  NA
close(con)