本文介绍了从两个数值向量生成逻辑向量-R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R很陌生,很难尝试从两个大小相同(任意)的数字向量a和b生成逻辑向量c,其中c包含TRUE或FALSE取决于向量b的对应元素是a的倍数.

I'm very new to R and having difficulty trying to generate a logical vector c from two numeric vectors a and b of the same (arbitrary) size where c contains either TRUE or FALSE depending on whether the corresponding elements of vector b are multiples of a.

例如

    a<-c(2,3)
    b<-c(6,14)

结果c为TRUE,否

这是到目前为止我得到的:

This is what I've got thus far:

    a<-c(2:10)
    b<-c(6:14)
    c<-(if(b%%a) as.integer(TRUE))

我意识到 if()函数不是向量化的,只会评估第一个元素,但是,我非常困惑,无法用其他任何方法找出其他结构成功.任何见解,链接或指导都将最有帮助.谢谢.

I realize that the if() function is not vectorized and will only evaluate the first element, however, I'm thoroughly confounded and have not been able to see figure out any other construction with any successes. Any insight, links, or guidance would be most helpful. Thanks.

推荐答案

只需执行以下操作:

c <- ( b%%a == 0 )

(括号是可选的,仅为了说明而包括在内.)

(The parentheses are optional, only included for clarity.)

这篇关于从两个数值向量生成逻辑向量-R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 00:42