1

I am doing practicing exercise based on the problems and solutions for data.table in R. The problem was: get the row and column positions of missing values in a data table. The solution code is used " [.....with=F][[1]]. I am not understanding this part of that code and expecting expert opinion to make my concept clear on that.

for(i in 1:NROW(DT)){
  for(j in 1:NCOL(DT)){
    curr_value <- DT[i, j,with=F][[1]]

I can understand first two lines, but not understanding ,with=F and then [[1]] What the meaning of with=F and why has been used [[1]] after than that. Why there is double bracket with 1?

Nazrul
  • 21
  • 5

1 Answers1

3

Generally in data.table, with = FALSE allows you to select columns named in a variable.

Consider the following minimal example,

library(data.table)
dt <- data.table(mtcars)

Let's select the following columns from dt

cols <- c(1, 7)

The following command will produce an error

dt[, cols]

Instead you can use with = F

dt[, cols, with = F]

From ?data.table

When with=TRUE (default), j is evaluated within the frame of the data.table; i.e., it sees column names as if they are variables.

A shorter alternative is to use

dt[, ..cols]

See also Why does “..” work to pass column names in a character vector variable?

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • 1
    Thanks Maurits Evers for your detail explanation on With=F part. It is really helpful. You also elaborated it more with double dot ... Much Appreciated!! – Nazrul Oct 30 '19 at 04:39