[R] which LETTERS?

Duncan Murdoch murdoch.duncan at gmail.com
Mon Nov 10 14:06:56 CET 2014


On 10/11/2014 7:50 AM, Charles Stangor wrote:
> I'm confused:
>
> Thanks in advance.
>
> > which(LETTERS == c("A"))

This computes

LETTERS == c("A")

then returns the indices where it is TRUE.  Since LETTERS has 26 
elements, but "A" has only one, the "A" is repeated 26 times.  Only the 
first one matches LETTERS.
> [1] 1
> > which(LETTERS == c("A","B"))

The c("A", "B") needs to be repeated 13 times to get to length 26. Only 
the first two match.
> [1] 1 2
> > which(LETTERS == c("A","B","C"))

c("A", "B", "C") can't be repeated a whole number of times to extend to 
length 26, so it is repeated 8 2/3 times, and you get a warning.
> [1] 1 2 3
> Warning message:
> In LETTERS == c("A", "B", "C") :
>    longer object length is not a multiple of shorter object length

You probably want to use

which(LETTERS %in% c("A","B","C"))

instead.

Duncan Murdoch


>
> Charles Stangor
> Professor
> Dept of Psychology
> University of Maryland
> Academic Achievement Research Group <http://www.charlesstangor.com/AARG>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.



More information about the R-help mailing list