[英]rename a vector in reverse in r
我想重命名與我的向量不匹配的變量的 rest,但我不知道合適的代碼是什么,我添加一個示例。
example<-c("a","b","c","d","e")
keep<-c("b","c","d")
example[example %in% -keep] <- "vocal"
否定 ( !
) 而不是-
反轉邏輯 output 即TRUE -> FALSE
反之亦然
example[!example %in% keep] <- "vocal"
如果我們想使用-
,用which
換行以獲得 position 索引,然后使用
example[-which(example %in% keep)] <- "vocal"
但這是有問題的,在某些情況下可能會出現意外的 output。 例如,如果沒有匹配項
> keep1 <- c("f", "l")
> which(example %in% keep1)
integer(0)
> -which(example %in% keep1)
integer(0)
> example[-which(example %in% keep1)] <- "vocal"
> example
[1] "a" "b" "c" "d" "e"
在這里,它們都沒有改變,因為which
沒有返回任何元素,這對-
沒有影響
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.