Hello, I must be blind not to see it, but I have the following vector: 4 4 5 6 6 4 What I would like to have as a result is: 4 5 6 4 All repeated values are gone. I cannot use unique for this, as the second 4 would disappear. Is there another fast function for this problem? Thanks in advance! -- View this message in context: http://n4.nabble.com/delete-repeated-values-not-unique-tp1557625p1557625.html Sent from the R help mailing list archive at Nabble.com.
On Feb 16, 2010, at 11:01 AM, jorgusch wrote:> > Hello, > > I must be blind not to see it, but I have the following vector: > > 4 > 4 > 5 > 6 > 6 > 4 > > What I would like to have as a result is: > > 4 > 5 > 6 > 4 > > All repeated values are gone. I cannot use unique for this, as the second 4 > would disappear. Is there another fast function for this problem? > > Thanks in advance!See ?rle x <- c(4, 4, 5, 6, 6, 4)> rle(x)$values[1] 4 5 6 4 HTH, Marc Schwartz
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of jorgusch > Sent: Tuesday, February 16, 2010 9:01 AM > To: r-help at r-project.org > Subject: [R] delete repeated values - not unique... > > > Hello, > > I must be blind not to see it, but I have the following vector: > > 4 > 4 > 5 > 6 > 6 > 4 > > What I would like to have as a result is: > > 4 > 5 > 6 > 4 > > All repeated values are gone. I cannot use unique for this, > as the second 4 > would disappear. Is there another fast function for this problem?Is this what you want? It keeps only those values which are the not the same as the previous value in the vector. > isFirstInRun <- function(x) c(TRUE, x[-1]!=x[-length(x)]) > data <- c(4, 4, 5, 6, 6, 4) > data[isFirstInRun(data)] [1] 4 5 6 4 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > Thanks in advance! > > -- > View this message in context: > http://n4.nabble.com/delete-repeated-values-not-unique-tp1557625p1557625.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
Well, can you algorithmically describe what you are trying to do? Your example is not sufficient to determine it. For instance, are you trying to: 1) remove repeated elements of a vector and concatenate the first element at the end? 2) remove repeated elements of a vector and concatenate the minimum element at the end? 3) always return the vector c(4, 5, 6, 4) ? 4) something else? jorgusch wrote:> Hello, > > I must be blind not to see it, but I have the following vector: > > 4 > 4 > 5 > 6 > 6 > 4 > > What I would like to have as a result is: > > 4 > 5 > 6 > 4 > > All repeated values are gone. I cannot use unique for this, as the second 4 > would disappear. Is there another fast function for this problem? > > Thanks in advance! >
On Feb 16, 2010, at 12:01 PM, jorgusch wrote:> > Hello, > > I must be blind not to see it, but I have the following vector: > > 4 > 4 > 5 > 6 > 6 > 4 > > What I would like to have as a result is: > > 4 > 5 > 6 > 4 >?diff > vec <- c(4,4,5,6,6,4) > vec[ c(1, diff(vec)) != 0 ] [1] 4 5 6 4> All repeated values are gone. I cannot use unique for this, as the > second 4 > would disappear. Is there another fast function for this problem? > > Thanks in advance! > > -- > View this message in context: http://n4.nabble.com/delete-repeated-values-not-unique-tp1557625p1557625.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT
rle() is cool. ----- PhD Candidate in Statistics Dept. of Mathematics & Statistics University of New Hampshire Durham, NH 03824 USA -- View this message in context: http://n4.nabble.com/delete-repeated-values-not-unique-tp1557625p1557728.html Sent from the R help mailing list archive at Nabble.com.
rle is extremely cool!! This is really an impressive method to reduce vectors to point where you need to do calculations. This really helped! Thanks a lot! jorgusch -- View this message in context: http://n4.nabble.com/delete-repeated-values-not-unique-tp1557625p1559171.html Sent from the R help mailing list archive at Nabble.com.