[R] R command to compare two vectors?

Gabor Grothendieck ggrothendieck at gmail.com
Thu Feb 5 01:07:13 CET 2009


Here is one other way:

bnota <- setdiff(b_tmp, a_tmp)

gives the members of b_tmp that are not in a_tmp so length(bnota) == 0
if all of b_tmp is in a_tmp.

On Wed, Feb 4, 2009 at 6:56 PM, Jorge Ivan Velez
<jorgeivanvelez at gmail.com> wrote:
> Dear Jason,
> Yes, here is one way:
>
>> a_tmp<-c("a", "b", "c", "d", "e", "f", "g", 'h')
>> b_tmp<-c("a", "c", "e", "g")
>> b_tmp %in% a_tmp
> [1] TRUE TRUE TRUE TRUE
>> b_tmp[b_tmp%in%a_tmp]
> [1] "a" "c" "e" "g"
>> all(b_tmp %in% a_tmp)
> [1] TRUE
>
> Take a look at ?"%in%" and ?all for more information.
>
> HTH,
>
> Jorge
>
>
> On Wed, Feb 4, 2009 at 6:49 PM, Jason Rupert <jasonkrupert at yahoo.com> wrote:
>
>> By any chance is there an R command to compare two vectors?
>>
>> For example,
>> a_tmp<-c("a", "b", "c", "d", "e", "f", "g", 'h')
>> b_tmp<-c("a", "c", "e", "g")
>>
>> I would like to compare b_tmp against a_tmp to determine if the members of
>> b_tmp are part of a_tmp.
>>
>> I tried
>>  subset(b_tmp, b_tmp==a_tmp)
>>
>> That doesn't seem to work.
>>
>> Thanks again.
>>
>>
>>
>>        [[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.
>>
>>
>
>        [[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