[R] Vector grouping challenge

Johannes Graumann johannes_graumann at web.de
Wed Oct 28 13:43:32 CET 2009


Just so. I got until 'split' but was stuck on how to get the breaks ...

Thank you!

Joh

jim holtman wrote:

> Is this what you want:
> 
>>  testVector <- c(12,32,NA,NA,56,NA,78,65,87,NA,NA,NA,90)
>>  # get the breaks at the NAs
>>  xb <- cumsum(!is.na(testVector))
>>  split(seq(length(testVector)), xb)
> $`1`
> [1] 1
> 
> $`2`
> [1] 2 3 4
> 
> $`3`
> [1] 5 6
> 
> $`4`
> [1] 7
> 
> $`5`
> [1] 8
> 
> $`6`
> [1]  9 10 11 12
> 
> $`7`
> [1] 13
> 
> 
> On Wed, Oct 28, 2009 at 7:57 AM, Johannes Graumann
> <johannes_graumann at web.de> wrote:
>> Dear all,
>>
>> Is there an efficient way to get this list
>>> testList <- list(c(1),c(2,3,4),c(5,6),c(7),c(8),c(9,10,11,12),c(13))
>>
>> from this vector
>>> testVector <- c(12,32,NA,NA,56,NA,78,65,87,NA,NA,NA,90)
>> ?
>>
>> Basically the vector should be grouped, such that non-NA and all
>> following NAs end up in one group.
>>
>> Thanks for any hint,
>>
>> Joh
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html and provide commented,
>> minimal, self-contained, reproducible code.
>>
> 
> 
>




More information about the R-help mailing list