2015-09-20 90 views
2

請參閱下面的代碼。 id <- 1:10。如何避免警告?檢查R中的矢量值

allFiles <- list.files(directory) 

fileRange <- c(1:length(allFiles)) 
if(!(as.numeric(id) %in% fileRange)) 
{ 
    print("Invalid file range") 
    stop() 
} 
Warning: the condition has length > 1 and only the first element will be used 
+5

也許你想'如果(所有(我d%in%seq_along(fileRange)))'? – Frank

+0

@Frank是的。感謝它的工作。 –

回答

6

要檢查一個向量是否完全位於內的另一個,使用all

if (all(x %in% y)) 
0

其他道路:

if (all(is.element(x, y))) 

if (setequal(x, intersect(x, y)))