R: Subsetting a list based on a condition
Quite a handy couple of lines of code to subset a list in R to just those elements which meet a certain condition. Here’s an example to return only those elements of a list which are a certain class.
Thanks to this StackOverflow answer.
list.condition <- sapply(input.list, function(x) class(x)=="desired.class")
output.list <- input.list[list.condition]
To leave a comment
for the author, please follow the link and comment on his blog: Drunks&Lampposts » R
offers daily e-mail updates
news and tutorials
on topics such as: visualization (ggplot2
), programming (RStudio
, Web Scraping
) statistics (regression
, time series
) and more...
If you got this far, why not subscribe for updates
from the site? Choose your flavor: e-mail
, or facebook