How to remove an unnamed element from a single item list?
Using nzchar
.
lapply(v2, function(x) x[nzchar(x)])
# [[1]]
# [1] "1" "2" "3"
Or use base::strsplit
in the first place which appears to be more sophisticated.
lapply(strsplit(v[[1]], ","), trimws)
# [[1]]
# [1] "1" "2" "3"
You can use Filter
with nchar
v2 <- lapply(str_split(v, pattern = ",\\s?"), Filter, f = nchar)
which gives
> v2
[[1]]
[1] "1" "2" "3"
An option is also setdiff
to remove the ""
lapply(str_split(v, pattern = ",\\s*"), setdiff, "")
#[[1]]
#[1] "1" "2" "3"