[R] Odp: (no subject)

Petr PIKAL petr.pikal at precheza.cz
Fri Jan 28 16:37:52 CET 2011


Hi

r-help-bounces at r-project.org napsal dne 28.01.2011 15:37:07:

> Hello,
> I have a data set with each column containing data like this:
> row column1
> 1 1.2 
> 2 NA 
> 3 NA 
> 4 3 
> 5 5
> 6 NA 
> 7 1.5 
> 8 NA 
> 9 NA 
> 10  NA 
> 11 3 
> 12 NA 
> 13  3 
> 14  NA 
> 15 NA 
> 16 NA 
> 17 NA 
> 18 3 
> 19 1.2 
> 20 NA
> 
> >From this I would like to extract the sequence containing at least 
three rows
> of 3 in a row (exlcuding NAs). I would also like to have the row numbers 

> showing the place of the last number unequal to 3 befor the first 3 in 
the 
> sequence and the place of the next number after the last 3 in the 
sequence.
> So in this example I would get  11 to 18 as the sequence rows and row 7 
to 19 

Why do you exclude 3 in line 4? If you want everything between first and 
last occurrence of some number (3) you can use

first<-which(cumsum(some.vector%in%3)==1)
last<-which.max(cumsum(some.vector%in%3))
some.vector[first:last]

> as the rows showing the numbers before and after that are unequal to 
three.

This can be little tricky as just before 3 there can be also NA. So if you 
want this NA it is straightforward but if you want first real number you 
has to use is.na.

First numeric values in vector before first occurrence of 3

which.max(cumsum(!is.na(some.vector[1:(first-1)])))

first numeric value after last 3

last+which(cumsum(!is.na(some.vector[(last+1):50]))==1)

Regards
Petr

> The rle does not work since it is not possible to exlude the NAs.
> 
> I hope you understand the question. All help is highly appreciated.
> 
> /Marine
> ______________________________________________
> 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