[R] Vector grouping challenge
jholtman at gmail.com
Wed Oct 28 13:50:09 CET 2009
What the breaks are is that you are looking to see where the NAs
start. In your case, you wanted the value at the start of the NA
string to be grouped with the following NAs. The 'is.na' will return
TRUE for NAs and if you invert the vector, you will have TRUE for each
of the non-NA values. By doing the 'cumsum' you will get the same
values for the NAs that follow a non-NA:
 FALSE FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE TRUE
 1 2 2 2 3 3 4 5 6 6 6 6 7
The result of the 'cumsum' is then then grouping factor for split.
On Wed, Oct 28, 2009 at 8:43 AM, Johannes Graumann
<johannes_graumann at web.de> wrote:
> Just so. I got until 'split' but was stuck on how to get the breaks ...
> Thank you!
> 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
>>  2 3 4
>>  5 6
>>  7
>>  8
>>  9 10 11 12
>>  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,
>>> R-help at r-project.org mailing list
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html and provide commented,
>>> minimal, self-contained, reproducible code.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help