On Thu, 29 Apr 2010, Barry Rowlingson wrote:
On Thu, Apr 29, 2010 at 1:27 PM, Henrique Dallazuanna <www...@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:cbe...@tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
______________________________________________
R-help@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.