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 as the rows showing the numbers before and after that are unequal to three. 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
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 leastthree 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 inthe> sequence and the place of the next number after the last 3 in thesequence.> So in this example I would get 11 to 18 as the sequence rows and row 7to 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 tothree. 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
This looks like homework. We don't do homework. Certainly not when you obviously haven't a) read the posting guide, b) asked your professor/tutor, c) googled to see if you find something online, d) at least made an effort to try, and e) read the posting guide. Do all the points above and then try posting again. Siterer "Marine Andersson" <Marine.Andersson at ki.se>:> 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 as the rows showing the numbers before and after that > are unequal to three. > 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. >