Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove empty strings in a list of lists in R

Tags:

list

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

like image 235
mathilde Avatar asked Dec 22 '22 20:12

mathilde


1 Answers

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.

like image 57
Cettt Avatar answered Jan 06 '23 09:01

Cettt