Dear Michael,
Thank you for your clear explanation.
Best regards,
Jorge.-

On Thu, Jan 12, 2012 at 10:34 PM, R. Michael Weylandt <> wrote:

> As Jorge noted, the fix is to use "%in%": a fuller explanation of why
> `==` didn't work is that it implicitly used vector recycling: look at
>
> with(data, id == c("a", "c"))
>
> implicitly, this expands to id == c("a","c", "a", "c") to get the
> lengths to match. Obviously only the first elements work here.
>
> But when you had c("a", "d") it expanded to c("a","d", "a", "d") and
> you get TRUE for the 1st and 4th slot. This, however, was just a lucky
> coincidence. Had you used c("d", "a") there would have been no
> matches.
>
> Anyways, definitely use %in% but hopefully this clarifies things.
>
> Michael
>
> On Thu, Jan 12, 2012 at 9:50 PM, Jorge I Velez <> wrote:
> > Hi,
> >
> > Use %in% instead of ==.
> >
> > HTH,
> > Jorge.-
> >
> >
> > On Thu, Jan 12, 2012 at 9:36 PM, 孟欣 <> wrote:
> >
> >> Hi all
> >> I have a question about "subset" function.
> >>
> >>
> >> > dat
> >>  id x1 x2  x3
> >> 1  a  1 11 111
> >> 2  b  2 22 222
> >> 3  c  3 33 333
> >> 4  d  4 44 444
> >>
> >>
> >> > subset(dat,id==c("a","c"))
> >>  id x1 x2  x3
> >> 1  a  1 11 111
> >>
> >> > subset(dat,id==c("a","d"))
> >>  id x1 x2  x3
> >> 1  a  1 11 111
> >> 4  d  4 44 444
> >>
> >>
> >> >From the above, if I choose id=a,c, the result is wrong,but if I choose
> >> id=a,d, the result is right.
> >>
> >>
> >> What's the reason for it?
> >>
> >>
> >>
> >>
> >> Many thanks!
> >>
> >>
> >>
> >>
> >> My best
> >>
> >>
> >>        [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> R-help@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.
> >>
> >
> >        [[alternative HTML version deleted]]
> >
> >
> > ______________________________________________
> > R-help@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.
> >
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to