I have two variables, both numerical. I would like to find the unique values of the pairs, in other words, unique coordinates if I were to plot them. I also need to know how many pairs there are, but I guess I can use length() if I can somehow isolate the unique pairs first? Thanks a lot! Bonnie Yuan
Hi Bonnie, ?unique probably would have done the trick for you. But here's an example:> z <- data.frame(x = rep(1:5, each=2), y = rep(1:2, each=5)) > zx y 1 1 1 2 1 1 3 2 1 4 2 1 5 3 1 6 3 2 7 4 2 8 4 2 9 5 2 10 5 2> unique(z)x y 1 1 1 3 2 1 5 3 1 6 3 2 7 4 2 9 5 2> nrow(unique(z))[1] 6 Sarah On Thu, Sep 15, 2011 at 12:27 PM, <bby2103 at columbia.edu> wrote:> I have two variables, both numerical. I would like to find the unique values > of the pairs, in other words, unique coordinates if I were to plot them. > > I also need to know how many pairs there are, but I guess I can use length() > if I can somehow isolate the unique pairs first? > > Thanks a lot! > > Bonnie Yuan > > ______________________________________________ > 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. >-- Sarah Goslee http://www.functionaldiversity.org
Bonnie: 1. As usual, see FAQ 7.31. unique() may not mean what you think it means for finite precision numbers:> unique(c(1/3, sqrt(2)*1/3/sqrt(2)))[1] 0.3333333 0.3333333 But modulo that: unique(paste(v1,v2)) will give you what you're looking for I think. -- Bert On Thu, Sep 15, 2011 at 9:27 AM, <bby2103 at columbia.edu> wrote:> I have two variables, both numerical. I would like to find the unique values > of the pairs, in other words, unique coordinates if I were to plot them. > > I also need to know how many pairs there are, but I guess I can use length() > if I can somehow isolate the unique pairs first? > > Thanks a lot! > > Bonnie Yuan > > ______________________________________________ > 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. >-- "Men by nature long to get on to the ultimate truths, and will often be impatient with elementary studies or fight shy of them. If it were possible to reach the ultimate truths without the elementary studies usually prefixed to them, these would not be preparatory studies but superfluous diversions." -- Maimonides (1135-1204) Bert Gunter Genentech Nonclinical Biostatistics
On Sep 15, 2011, at 12:27 PM, bby2103 at columbia.edu wrote:> I have two variables, both numerical. I would like to find the > unique values of the pairs, in other words, unique coordinates if I > were to plot them. > > I also need to know how many pairs there are, but I guess I can use > length() if I can somehow isolate the unique pairs first??duplicated dfrm[ !duplicated(dfrm), ] # Would give the unique pairs. -- David Winsemius, MD West Hartford, CT