Peng Yu
2009-Dec-02 21:33 UTC
[R] documentation of intersect() on string vector and num vector and on duplicated elements
> intersect(c(1,3,2),c('1','3'))[1] "1" "3" Apparently, intersect() treats num as string. But this is not documented in the help. Could somebody add it in the future version of R? Also according to the help, the argument should not have duplicated elements. But I tried the following example, it seems that it doesn't matter where there are duplicated elements or not. Should the help be corrected? Or somebody could provide me with an example that make intersect() fails if there are duplicated elements?> x=c(3,1,2,1,7) > y=c(5,1,4,2,3,8,1) > x[1] 3 1 2 1 7> y[1] 5 1 4 2 3 8 1> intersect(x,y)[1] 3 1 2
Erik Iverson
2009-Dec-02 21:48 UTC
[R] documentation of intersect() on string vector and num vector and on duplicated elements
> > intersect(c(1,3,2),c('1','3')) > [1] "1" "3" > > Apparently, intersect() treats num as string. But this is not > documented in the help. Could somebody add it in the future version of > R? >This is more a general coercion feature. For example 1 == "1", see ?Comparison.> Also according to the help, the argument should not have duplicated > elements.I think this is just because these functions were written with sets in mind, which would not have duplicated values, thus the word "conceptually". The duplicated elements are simply discarded.
David Winsemius
2009-Dec-02 21:51 UTC
[R] documentation of intersect() on string vector and num vector and on duplicated elements
On Dec 2, 2009, at 4:33 PM, Peng Yu wrote:>> intersect(c(1,3,2),c('1','3')) # note x is numeric and y is >> character > [1] "1" "3" > > Apparently, intersect() treats num as string. But this is not > documented in the help. Could somebody add it in the future version of > R?It is documented that intersect will return a value of that is same mode as its "y" argument. How could it be any more clear?> > Also according to the help, the argument should not have duplicated > elements.That's not what my help page says. It says that "conceptually" they won't have them, but immediately thereafter it says: "Details Each of union, intersect, setdiff and setequal will discard any duplicated values in the arguments, and they apply as.vector to their arguments (and so in particular coerce factors to character vectors)." Unfortunately, R does not have a package that will ensure that users will read the help pages carefully. -- David> But I tried the following example, it seems that it doesn't > matter where there are duplicated elements or not. Should the help be > corrected? Or somebody could provide me with an example that make > intersect() fails if there are duplicated elements? > >> x=c(3,1,2,1,7) >> y=c(5,1,4,2,3,8,1) >> x > [1] 3 1 2 1 7 >> y > [1] 5 1 4 2 3 8 1 >> intersect(x,y) > [1] 3 1 2 > > ______________________________________________ > 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
Erik Iverson
2009-Dec-02 21:55 UTC
[R] documentation of intersect() on string vector and num vector and on duplicated elements
> > It is documented that intersect will return a value of that is same > mode as its "y" argument. How could it be any more clear? >Oops, this makes my previous response to this question inaccurate, apologies.