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

2010-04-29 Thread Romain Francois
Maybe this : foo - function( x ){ + idx - 1 + cumsum( is.na( x ) ) + not.na - ! is.na( x ) + split( x[not.na], idx[not.na] ) + } foo( x ) $`1` [1] 2 1 2 $`2` [1] 1 1 2 $`3` [1] 4 5 2 3 Romain Le 29/04/10 09:42, Tal Galili a écrit : Hi all, I would like to have a function like

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

2010-04-29 Thread Tal Galili
Definitely Smarter, Thanks! Tal Contact Details:--- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English)

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

2010-04-29 Thread Henrique Dallazuanna
Another option could be: split(x, replace(cumsum(is.na(x)), is.na(x), -1))[-1] On Thu, Apr 29, 2010 at 4:42 AM, Tal Galili tal.gal...@gmail.com wrote: Hi all, I would like to have a function like this: split.vec.by.NA - function(x) That takes a vector like this: x -

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

2010-04-29 Thread Barry Rowlingson
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

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

2010-04-29 Thread Charles C. Berry
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)

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

2010-04-29 Thread Thomas Stewart
Or, you can modify Romain's function to account for sequential NAs. x - c(1,2,NA,1,1,2,NA,NA,4,5,2,3) foo - function( x ){ idx - 1 + cumsum( is.na( x ) ) not.na - ! is.na( x ) f-factor(idx[not.na],levels=1:max(idx)) split( x[not.na], f ) } $`1` [1] 1 2 $`2` [1] 1 1 2 $`3`