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

Charles C. Berry cberry at tajo.ucsd.edu
Thu Apr 29 17:20:16 CEST 2010


On Thu, 29 Apr 2010, Barry Rowlingson wrote:

> 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!

The ever useful rle() helps

> y <- rle(!is.na(x))
> split(x, rep( cumsum(y$val)*y$val, y$len ) )[-1]
$`1`
[1] 1 2 3

$`2`
[1] 4 5 6


Chuck

>
> Barry
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list