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
On Thu, Dec 10, 2009 at 10:00 PM, Marc Schwartz marc_schwa...@me.com wrote:
On Dec 10, 2009, at 9:44 PM, Peng Yu wrote:
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)
Is there a version of apply that returns a list without NULL's?
I try to remove NULL elements in the following example, but neither
for loops work. Would you please let me know what the correct way is?
Try this function:
compact - function(x) Filter(Negate(is.null), x)
compact(x)
Hadley
3 matches
Mail list logo