I'm confused: Thanks in advance.> which(LETTERS == c("A"))[1] 1> which(LETTERS == c("A","B"))[1] 1 2> which(LETTERS == c("A","B","C"))[1] 1 2 3 Warning message: In LETTERS == c("A", "B", "C") : longer object length is not a multiple of shorter object length Charles Stangor Professor Dept of Psychology University of Maryland Academic Achievement Research Group <http://www.charlesstangor.com/AARG> [[alternative HTML version deleted]]
Hi Charles,
I think you're looking for %in%:
which(LETTERS %in% c("A", "B", "C"))
See ?"%in%" for details
Basically, with "==", the vector c("A","B") or
c("A","B","C") will be
recycled and compared with LETTERS, which is not what you want. You want
to match() LETTERS with the vector.
Maybe someone who better knows the correct terms will be able to explain
better how it works.
HTH,
Ivan
--
Ivan Calandra, ATER
University of Reims Champagne-Ardenne
GEGENA? - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
ivan.calandra at univ-reims.fr
https://www.researchgate.net/profile/Ivan_Calandra
Le 10/11/14 13:50, Charles Stangor a ?crit :> I'm confused:
>
> Thanks in advance.
>
>> which(LETTERS == c("A"))
> [1] 1
>> which(LETTERS == c("A","B"))
> [1] 1 2
>> which(LETTERS == c("A","B","C"))
> [1] 1 2 3
> Warning message:
> In LETTERS == c("A", "B", "C") :
> longer object length is not a multiple of shorter object length
>
>
> 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.
>
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 lengthYou 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.