我一直在使用R which函数从数据框中删除行.我最近发现,如果搜索词不在data.frame中,则结果为空字符.
# 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")]
Run Code Online (Sandbox Code Playgroud)
第二个例子-which()是未找到搜索词时的预期行为吗?我已经将代码切换为使用示例4中的语法,这似乎更安全,但我只是好奇.