1

This answer warns of some scary behavior from which. Specifically, if you take any data frame, say df <- data.frame(x=1:5, y=2:6), and then try to subset it with something that evaluates to which(FALSE) (i.e. integer(0)), then you will delete every column in the data set. Why is this? Why would dropping all columns that correspond to integer(0) delete everything? Deleting nothing shouldn't destroy everything.

Example:

>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
Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
J. Mini
  • 1,868
  • 1
  • 9
  • 38

1 Answers1

2

Consider:

identical(integer(0), -integer(0))
# [1] TRUE

So, actually you're selecting nothing, rather than deleting nothing.

If you want to delete nothing, you could use a large negative integer, e.g. the largest possible.

df[, -.Machine$integer.max]
#   x y
# 1 1 2
# 2 2 3
# 3 3 4
# 4 4 5
# 5 5 6
jay.sf
  • 60,139
  • 8
  • 53
  • 110