Atte Tenkanen
2006-Aug-16 07:42 UTC
[R] How to remove similar successive objects from a vector?
Is there some (much) more efficient way to do this? VECTOR=c(3,2,4,5,5,3,3,5,1,6,6); NEWVECTOR=VECTOR[1]; for(i in 1:(length(VECTOR)-1)) { if((identical(VECTOR[i], VECTOR[i+1]))==FALSE){ NEWVECTOR=c(NEWVECTOR,VECTOR[i+1])} }> VECTOR[1] 3 2 4 5 5 3 3 5 1 6 6> NEWVECTOR[1] 3 2 4 5 3 5 1 6 _______________________________ Atte Tenkanen University of Turku, Finland
Dimitris Rizopoulos
2006-Aug-16 07:57 UTC
[R] How to remove similar successive objects from a vector?
try something like the following: x <- c(3,3,2,4,5,5,3,3,5,1,6,6) ######### nx <- length(x) ind <- c(TRUE, (x[1:(nx-1)] - x[2:nx]) != 0) x[ind] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Atte Tenkanen" <attenka at utu.fi> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, August 16, 2006 9:42 AM Subject: [R] How to remove similar successive objects from a vector?> Is there some (much) more efficient way to do this? > > VECTOR=c(3,2,4,5,5,3,3,5,1,6,6); > NEWVECTOR=VECTOR[1]; > > for(i in 1:(length(VECTOR)-1)) > { > if((identical(VECTOR[i], VECTOR[i+1]))==FALSE){ > NEWVECTOR=c(NEWVECTOR,VECTOR[i+1])} > } > >> VECTOR > [1] 3 2 4 5 5 3 3 5 1 6 6 >> NEWVECTOR > [1] 3 2 4 5 3 5 1 6 > > _______________________________ > Atte Tenkanen > University of Turku, Finland > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Jacques VESLOT
2006-Aug-16 08:01 UTC
[R] How to remove similar successive objects from a vector?
VECTOR=c(3,2,2,3,4,4,5,5,5,3,3,3,5,1,6,6) NEWVECTOR <- ifelse(VECTOR[-length(VECTOR)]==VECTOR[-1],NA,VECTOR) NEWVECTOR[!is.na(NEWVECTOR)] [1] 3 2 3 4 5 3 5 1 ------------------------------------------------------------------- Jacques VESLOT CNRS UMR 8090 I.B.L (2?me ?tage) 1 rue du Professeur Calmette B.P. 245 59019 Lille Cedex Tel : 33 (0)3.20.87.10.44 Fax : 33 (0)3.20.87.10.31 http://www-good.ibl.fr ------------------------------------------------------------------- Atte Tenkanen a ?crit :> Is there some (much) more efficient way to do this? > > VECTOR=c(3,2,4,5,5,3,3,5,1,6,6); > NEWVECTOR=VECTOR[1]; > > for(i in 1:(length(VECTOR)-1)) > { > if((identical(VECTOR[i], VECTOR[i+1]))==FALSE){ > NEWVECTOR=c(NEWVECTOR,VECTOR[i+1])} > } > > >>VECTOR > > [1] 3 2 4 5 5 3 3 5 1 6 6 > >>NEWVECTOR > > [1] 3 2 4 5 3 5 1 6 > > _______________________________ > Atte Tenkanen > University of Turku, Finland > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Jarimatti Valkonen
2006-Aug-16 08:05 UTC
[R] How to remove similar successive objects from a vector?
On Wed, Aug 16, 2006 at 10:42:35AM +0300, Atte Tenkanen wrote:> Is there some (much) more efficient way to do this? > > VECTOR=c(3,2,4,5,5,3,3,5,1,6,6); > NEWVECTOR=VECTOR[1]; > > for(i in 1:(length(VECTOR)-1)) > { > if((identical(VECTOR[i], VECTOR[i+1]))==FALSE){ > NEWVECTOR=c(NEWVECTOR,VECTOR[i+1])} > } > > > VECTOR > [1] 3 2 4 5 5 3 3 5 1 6 6 > > NEWVECTOR > [1] 3 2 4 5 3 5 1 6How about rle? rle(VECTOR)$values should do the same thing. Don't know about efficiency, though. -- Jarimatti Valkonen
Gavin Simpson
2006-Aug-16 08:05 UTC
[R] How to remove similar successive objects from a vector?
On Wed, 2006-08-16 at 10:42 +0300, Atte Tenkanen wrote:> Is there some (much) more efficient way to do this? > > VECTOR=c(3,2,4,5,5,3,3,5,1,6,6); > NEWVECTOR=VECTOR[1]; > > for(i in 1:(length(VECTOR)-1)) > { > if((identical(VECTOR[i], VECTOR[i+1]))==FALSE){ > NEWVECTOR=c(NEWVECTOR,VECTOR[i+1])} > } > > > VECTOR > [1] 3 2 4 5 5 3 3 5 1 6 6 > > NEWVECTOR > [1] 3 2 4 5 3 5 1 6 > > _______________________________ > Atte TenkanenIs this what you mean? x <- c(3, 2, 4, 5, 5, 3, 3, 5, 1, 6, 6) x.wanted <- c(3, 2, 4, 5, 3, 5, 1, 6) X <- x[diff(x) != 0] all.equal(x.wanted, X) # check it works G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% *Note new Address and Fax and Telephone numbers from 10th April 2006* %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC [f] +44 (0)20 7679 0565 UCL Department of Geography Pearson Building [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street London, UK [w] http://www.ucl.ac.uk/~ucfagls/cv/ WC1E 6BT [w] http://www.ucl.ac.uk/~ucfagls/ %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Atte Tenkanen
2006-Aug-16 10:01 UTC
[R] How to remove similar successive objects from a vector?
Thanks for all respondents! I wasn't precise enough, when I enclosed my example. In fact, I need a version which works with all kinds of symbolic data, not only with numbers. So these versions rle(VECTOR)$values and VECTOR=c(3,2,2,3,4,4,5,5,5,3,3,3,5,1,6,6) NEWVECTOR <- ifelse(VECTOR[-length(VECTOR)]==VECTOR[-1],NA,VECTOR) NEWVECTOR[!is.na(NEWVECTOR)] answered to my needs. I made a test and the first version was 2.5x faster with my data, but both works enough fast. Atte On Wed, 2006-08-16 at 08:58 +0100, Patrick Burns wrote:> I think > > rle(VECTOR)$values > > will get you what you want. > > Patrick Burns > patrick at burns-stat.com > +44 (0)20 8525 0696 > http://www.burns-stat.com > (home of S Poetry and "A Guide for the Unwilling S User") > > Atte Tenkanen wrote: > > >Is there some (much) more efficient way to do this? > > > >VECTOR=c(3,2,4,5,5,3,3,5,1,6,6); > >NEWVECTOR=VECTOR[1]; > > > >for(i in 1:(length(VECTOR)-1)) > >{ > > if((identical(VECTOR[i], VECTOR[i+1]))==FALSE){ > > NEWVECTOR=c(NEWVECTOR,VECTOR[i+1])} > >} > > > > > > > >>VECTOR > >> > >> > > [1] 3 2 4 5 5 3 3 5 1 6 6 > > > > > >>NEWVECTOR > >> > >> > >[1] 3 2 4 5 3 5 1 6 > > > >_______________________________ > >Atte Tenkanen > >University of Turku, Finland > > > >______________________________________________ > >R-help at stat.math.ethz.ch 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. > > > > > > > >