[R] Problem with subset

Charles C. Berry cberry at tajo.ucsd.edu
Fri Jun 6 19:54:06 CEST 2008


On Fri, 6 Jun 2008, Luca Mortarini wrote:

> Hi,
>     I am new to R and i am looking for a way to extract a subset from a
> vector.
> I have a vector of number oscillating around zero (a decreasing
> autocorrelation function) and i would like to extract only the first
> positive part of the function (from zero lag to the lag where the function
> inverts its sign for the first time).
> I have tried
>
> subset(myvector,myvector>0)
>
> but this obviously extract all the positive intervals not only the first one.
> Is there a logical statement i can use in subset? I prefer not to use an

For vector subsets you probably want "[". Because from help("[")

 	For ordinary vectors, the result is simply x[subset & !is.na(subset)].


But see

 	?rle

Something like

 	myvector[ 1 : rle( myvector >= 0 )$lengths[ 1 ] ]

should work.

HTH,

Chuck


> if statement that would probably slow down the code.
> Thanks a lot,
>                          Luca
>
>
> *********************************************************
> dr. Luca Mortarini               l.mortarini at isac.cnr.it
> Università del Piemonte Orientale
> Dipartimento di Scienze e Tecnologie Avanzate
>
> ______________________________________________
> 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.
>

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