[R] Split a vector by NA's - is there a better solution then a loop ?

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Apr 29 15:25:51 CEST 2010


On Thu, Apr 29, 2010 at 1:27 PM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> Another option could be:
>
> split(x, replace(cumsum(is.na(x)), is.na(x), -1))[-1]
>

One thing none of the solutions so far do (except I haven't tried
Tal's original code) is insert an empty group between adjacent NA
values, for example in:

 x = c(1,2,3,NA,NA,4,5,6)

 > split(x, replace(cumsum(is.na(x)), is.na(x), -1))[-1]
$`0`
[1] 1 2 3

$`2`
[1] 4 5 6

Maybe this never happens in Tal's case, or it's not what he wanted
anyway, but I thought I'd point it out!

Barry



More information about the R-help mailing list