Does it delete all lines in the R dataframe with [-0,] officially supported?

I found that I can delete all rows of an R dataframe by subsetting using the square brackets function and using -0 as the row index. However, I haven't been able to find any documentation that says this is the official behavior that I can count on in the future. Is this an official feature that I can use with confidence, expecting it to continue to function this way in future releases?

> df <- data.frame(c1=c(1,2,3),c2=c(2,3,4), c3=c(4,5,6))
> df
  c1 c2 c3
1  1  2  4
2  2  3  5
3  3  4  6  
> df[-0,]
[1] c1 c2 c3
<0 rows> (or 0-length row.names)

      

+3


source to share


1 answer


Check the documentation.

help("[.data.frame")

says (my attention):

When [and [[are used with two indices (x [i, j] and x [[i, j]])), they act as an indexing of the matrix [...]. Note that for each selected column, xj, usually (if it is not matrix) the resulting column will be xj [i] and therefore rely on the appropriate [method , see examples.

The next stop help("[")

, which says nothing about using 0 as an index, but points to the manual for defining the R language.



There we read (emphasis mine):

A special case is index zero, which has zero effects: x [0] is an empty vector and otherwise including zeros among positive or negative indices have the same effect as if they were omitted.

In short:

Since this behavior is documented, you can rely on DF[0,]

returning a data.frame with empty vector columns.

+3


source







All Articles