mathilde
mathilde

Reputation: 204

Remove empty strings in a list of lists in R

I am currently working on a project of text-mining in R, with a list of lists. I want to remove all the empty strings and the NA values of my list of lists and I haven't found a way. My data looks like this :

x <- list(c("", "alteryx", "confirme", "", "", "", "ans", "", ""))

The principal list refered to different texts. And each text is composed of a list of all the words. So when I remove a word, it becomes an empty string. I just want to delete it definitively and so reduce the length of my text's length.

I tried many different things, such as

stri_remove_empty_na(c(demande2[20])) which gave me the error Warning message: In stri_enc_toutf8(x) : argument is not an atomic vector; coercing and transformed my text as : result of stri_remove_empty_na

So please I need your help (and I'm sorry for my English, I'm French :))

Thanks in advance

Upvotes: 6

Views: 7651

Answers (1)

Cettt
Cettt

Reputation: 11981

you can use lapply and simple subsetting:

x <- list(c("", "alteryx", "confirme", "", "", "", "ans", "", ""))
lapply(x, function(z){ z[!is.na(z) & z != ""]})

[[1]]
[1] "alteryx"  "confirme" "ans"

lapply applies a function to every component of a list. In this case the function is a simple subsetting function.

Upvotes: 14

Related Questions