假設我有許多載體,一個充當鑰匙:檢查等價,別提爲了
key <- c("A", "B")
vec1 <- c("B", "A") #one that changes the order
vec2 <- c("A") #one that is less one
vec3 <- c("A", "B", "C") #one that is plus one
vec4 <- c("A", "B") #this one is identical
我要退房去如下:
if(vec [has all the elements of] key == TRUE)
所以,結果會去:
vec1 == TRUE
vec2 == FALSE
vec3 == FALSE
vec4 == TRUE
我試過all()
但是...
all(key %in% vec1) #TRUE (as hoped)
all(key %in% vec2) #FALSE (as hoped)
all(key %in% vec3) #TRUE (drat!)
all(key %in% vec3) #TRUE (as expected)
我能得到這份工作與多個條件進行:
all(key %in% vec1) & all(vec1 %in% key) #TRUE
all(key %in% vec2) & all(vec2 %in% key) #FALSE
all(key %in% vec3) & all(vec3 %in% key) #FALSE
all(key %in% vec4) & all(vec4 %in% key) #TRUE
我在想,如果有一個更優雅的方式?
'setequal'假設你沒有/關心重複的元素。 – Frank