0

Can anyone explain what this line t[exists,][1:6,] is doing in the code below and how that subsetting works?

t<-trees
t[1,1]= NA
t[5,3]= NA
t[1:6,]
exists<-complete.cases(t)
exists
t[exists,][1:6,]
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
user3022875
  • 8,598
  • 26
  • 103
  • 167
  • 1
    It looks like it's taking the first six rows of `t` after `t` has been subsetted to have the non-complete cases removed – Rich Scriven Oct 10 '14 at 17:01
  • By the way: you can achieve equivalent behavior with `head(t[exists, ], n = 6)`, or just `head(t[exists, ])` since 6 is the default anyway. – shadowtalker Oct 10 '14 at 17:44
  • it's doing `(t[exists,])[1:6,]` basically. Btw both `t` and `exists` are quite prominent functions in R, I would recommend other variable names :). – moodymudskipper Oct 24 '18 at 09:41

2 Answers2

1

The complete.cases function will check the data frame and will return a vector of TRUE and FALSE where a TRUE indicates a row with no missing data. The vector will be as long as there are rows in t.

The t[exits,] part will subset the data so that only rows where exists is true will be considered - the row that have missing data will be FALSE in exists and removed. The [1:6,] will only take the first 6 rows where there is no missing data.

John Paul
  • 12,196
  • 6
  • 55
  • 75
1

Some background

In R, [ is a function like any other. R parses t[exists, ] as

`[`(t, exists)  # don't forget the backticks!

Indeed you can always call [ with the backtick-and-parentheses syntax, or even crazier use it in constructions like

as.data.frame(lapply(t[exists, ], `[`, 1:6, ))

which, believe it or not, is (almost) equivalent to t[exists,][1:6,].

The same is true for functions like [[, $, and more exotic stuff like names<-, which is a special function to assign argument value to the names attribute of an object. We use functions like this all the time with syntax like

names(iris) <- tolower(names(iris))

without realizing that what we're really doing is

`names(iris)<-`(iris, tolower(names(iris))

And finally, you can type

?`[`

for documentation, or type

`[`

to return the definition, just like any other function.

What t[exists,][1:6,] does

The simple answer is that R parses t[exists,][1:6,] as something like:

  1. Get the value of t
  2. From the result of step 1, get the rows that correspond to TRUE elements of exists.
  3. From the result of step 2, get rows with row numbers in the vector 1:6, i.e. rows 1 through 6

The more complicated answer is that this is handled by the parser as:

`[`(`[`(t, exists, ), 1:6, ) # yes, this has blank arguments

which a human can interpret as

temporary_variable_1 <- `[`(t, exists, )
temporary_variable_2 <- `[`(temporary_variable_1, 1:6, )
print(temporary_variable_2)  # implicitly, sending an object by itself to the console will `print` that object

Interestingly, because you typically can't pass blank arguments in R, certain constructions are impossible with the bracket function, like eval(call("[", t, exists, )) which will throw an undefined columns selected error.

shadowtalker
  • 12,529
  • 3
  • 53
  • 96
  • a little correction, when doing `names(iris) <- tolower(names(iris))`, you're not doing `'names(iris)<-'(iris, tolower(names(iris))`, you're doing `iris <- 'names(iris)<-'(iris, tolower(names(iris))` (actually not quite, there's an intermediate step defining a `*tmp*` variable, which might pop up when using `subsitute` or doing funky things with environments) – moodymudskipper Oct 24 '18 at 09:35
  • `you typically can't pass blank arguments in R`, you'd have to be more precise, you're using the comment "# yes, this has blank arguments" a few lines above – moodymudskipper Oct 24 '18 at 09:45
  • Try this out, this is the reason of this behavior : `test <- function(x,y) {print(missing(y));print(nargs())}; test(1); test(1,)`. From `?nargs`: `When used inside a function body, nargs returns the number of arguments supplied to that function, including positional arguments left blank`. If you look at the code of `[.data.frame` you'll see it uses `nargs`. – moodymudskipper Oct 24 '18 at 09:53