[R] How to get rid of NULL in the result of apply()?

Peng Yu pengyu.ut at gmail.com
Fri Dec 11 04:44:13 CET 2009


The following code returns a list with the 2nd element as NULL. I'm
wondering what the best way to get rid of NULL element in an
'apply()'s result.

> lapply(1:3, function(x) {
+       if(x==2) {
+         return(NULL)
+       } else {
+         return(x)
+       }
+     }
+     )
[[1]]
[1] 1

[[2]]
NULL

[[3]]
[1] 3




More information about the R-help mailing list