This answer警告来自which
的恐怖行为。具体来说,如果您采用任何数据框,例如说df <- data.frame(x=1:5, y=2:6)
,然后尝试使用计算结果为which(FALSE)
(即integer(0)
)的子集对其进行子集化,那么您将删除数据中的每一列组。为什么是这样?为什么删除与integer(0)
相对应的所有列会删除所有内容?删除任何内容都不会破坏一切。
示例:
>df <- data.frame(x=1:5, y=2:6)
>df
x y
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6
>df <- df[,-which(FALSE)]
>df
data frame with 0 columns and 5 rows
答案 0 :(得分:2)
考虑:
identical(integer(0), -integer(0))
# [1] TRUE
因此,实际上,您什么都不选择,而不是什么都不删除。
如果您不想删除任何内容,则可以使用大的负整数,例如最大的可能。
df[, -.Machine$integer.max]
# x y
# 1 1 2
# 2 2 3
# 3 3 4
# 4 4 5
# 5 5 6