在for循环中使用if(any())

在for循环中使用if(any()),r,if-statement,for-loop,any,R,If Statement,For Loop,Any,我想生成这样一个循环: group1 = c(1,3,7,25) for (ii in 1:25){ if (ii == any(group1)){test = 5} else {test=1} } 我收到一条警告,警告我将“double”类型的参数强制为逻辑参数。结果是只使用了我的else语句。这意味着什么?我该如何解决这个问题?谢谢。您使用了错误的任何功能 any(iterable) 如果iterable的任何元素为True,则返回True。如果iterable为空,则返回F

我想生成这样一个循环:

group1 = c(1,3,7,25)

for (ii in 1:25){
    if (ii == any(group1)){test = 5} else {test=1}
}

我收到一条警告,警告我将“double”类型的参数强制为逻辑参数。结果是只使用了我的else语句。这意味着什么?我该如何解决这个问题?谢谢。

您使用了错误的任何功能

any(iterable) 

如果iterable的任何元素为True,则返回True。如果iterable为空,则返回False。

不清楚您想要什么。可能

 test <- c(1,5)[1+(1:25) %in% group1]

您要求
any()
确定向量
group1
的任何值是否为真。。。我想你可能想要
any(ii%在%group1中)
@Justin可能根本不需要
any
。?@joran by jove我想你可能是对的@奥桑德尼兹:这没有道理。只有一个
any
函数
any()
如果
iterable==0
或者iterable=c(0,0,0,0)或者如果所有参数都是
FALSE
,则返回FALSE,但如果它们是
FALSE
,则返回FALSE,这完全有效!谢谢你,德温!你是对的,我不清楚我的测试输出应该是一个向量。
test <- ifelse( 1:25 %in% group1, 5, 1)
test <- integer(25)
for (ii in 1:25){
   if (ii %in% group1){test[ii] <- 5} else {test[ii] <- 1}
                }
test