RScripting:在我的 xVec 和 yVec 中遇到的问题

RScripting: Problems in encountered in my xVec and yVec

任何人都可以帮助我获得正确的命令来查找可被 2 整除的数字并获取 xVec 中索引位置的元素吗?请参考下图。

顺便说一句,我使用 View() 命令分别检查了我的 View(xVec) 和 (yVec) 的项目,但是如果我想让它们在 (xVec) 下被 2 整除,我该如何找到这些数字?我应该使用命令 criteria.filter(xVec,/2) 还是只使用 >str_detect(

欢迎来到 R!在 R 中,二元运算符 %% 计算除法的余数,并且它是矢量化的,就像 R 中的大多数其他二元运算符一样。您可以这样做以获得可被 2:[=13 整除的向量元素=]

# The remainders
x_remainders <- xVec %% 2
# Get elements of xVec that is divisible by 2
x_div2 <- x[x_remainders == 0]
# Get indices within xVec where the element is divisible by 2
ind_x_div2 <- which(x_remainders == 0)

希望这对您有所帮助。 str_detect 可能不是最好的方法,因为您在这里使用的是数字而不是字符串。