我一直在使用R which
函數刪除數據框中的行。我最近發現如果搜索項不在data.frame中,結果是一個空字符。當搜索項未找到時,在R中使用-which()會出現意外的行爲
# 1: returns A-Q, S-Z (as expected)
LETTERS[-which(LETTERS == "R")]
# 2: returns "character(0)" (not what I would expect)
LETTERS[-which(LETTERS == "1")]
# 3: returns A-Z (expected)
LETTERS[which(LETTERS != "1")]
# 4: returns A-Q, S-Z (expected)
LETTERS[which(LETTERS != "R")]
是第二個例子爲-which()
預期的行爲時沒有找到搜索詞?我已經將代碼切換到使用示例4中的語法,這看起來更安全,但我只是好奇。