histelheim
histelheim

Reputation: 5098

How to delete "" from a list of character vectors

I have a list of character vectors, where some elements are actual strings, such as "FA" and "EX". However, some others are just "". I want to delete these.

list1 <- c("FA", "EX", "")
list2 <- c("FA")
list3 <- c("")
list <- list(list1, list2, list3)

> list
[[1]]
[1] "FA" "EX" ""  

[[2]]
[1] "FA"

[[3]]
[1] ""

Should then be

[[1]]
[1] "FA" "EX"  

[[2]]
[1] "FA"

How can I accomplish this?

Upvotes: 2

Views: 129

Answers (1)

akrun
akrun

Reputation: 887901

Try

lapply(list[list!=''], function(x) x[x!=''])

Upvotes: 5

Related Questions