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

2009-12-10 Thread Peng Yu
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

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

2009-12-10 Thread Peng Yu
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)

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

2009-12-10 Thread hadley wickham
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