R : Split a String into Vector & remove Empty stirngs

I would like to take a string of the following form and make it into a string vector. The following does not work, and I can’t follow the docs to figure it out:

params <- "-item1-map-item2-"
param_list <- as.list(strsplit(params, "-")[1])
[1] ""      "item1" "map"   "item2"

There is an annoying empty string at the start. I’ve tried to use

param_list <- stringi::stri_remove_empty_na(param_list)

but that throws an error:

> Warning message: In stri_enc_toutf8(x) : argument is not an atomic
> vector; coercing

Another suggested :

param_list <- param_list[nzchar(param_list) & complete.cases(param_list)]
[1] ""      "item1" "map"   "item2"

But no change… Could anyone shine a little light on this?

>Solution :


lapply(param_list, function(x) {x[!x == ""]})

[1] "item1" "map"   "item2"

Leave a Reply