Given a list with a null element:
l<-list(x=1,b=2,c=NULL)
How can I Reduce the list using '+' addition but avoid adding the NULL value? I tried
Reduce(l,"+",null.rm=T)
but I don't think its got null.rm. Any efficient way of solving this?
Thanks
You can do this with Filter.
Reduce('+', Filter(Negate(is.null), l))
This is also equivalent to tail(cumsum(Filter(Negate(is.null), l)), 1) or just good old-fashioned do.call(sum, l).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With