Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delete all elements of R list that are character

I have a list in R that looks like this:

> a = seq(1,10,2)
> b = 'test'
> c = seq(1,3,1)
> d = 'another test'
> 
> ls = list(a,b,c,d)
> 
> ls
[[1]]
[1] 1 3 5 7 9

[[2]]
[1] "test"

[[3]]
[1] 1 2 3

[[4]]
[1] "another test"

Is there a way to delete all list elements that are characters? So, the result would look like this:

[[1]]
[1] 1 3 5 7 9

[[2]]
[1] 1 2 3

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Not a good idea to call your list ls. It messes up with function ls. I call it lst below.

lst <- list(c(1, 3, 5, 7, 9), "test", c(1, 2, 3), "another test")

We can do

lst[!sapply(lst, is.character)]
#[[1]]
#[1] 1 3 5 7 9
#
#[[2]]
#[1] 1 2 3

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading