denis
denis

Reputation: 5673

Strange data.table column selection

I remarked a strange behavior of data.table that I don't understand:

library(data.table)
df <- as.data.table(matrix(ncol = 100,nrow = 3,data = sample(letters,300,replace = T)))

If I want to inverse first two columns, I could do:

df[,c(2,1,3:100L)]

which works fine. But if I do:

df[,c(2,1,3:ncol(df))]

  [1]   2   1   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32
 [33]  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64
 [65]  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96
 [97]  97  98  99 100

and I don't understand it, because ncol(df) is 100 and is an integer. Why does it do that ?

Upvotes: 2

Views: 65

Answers (1)

chinsoon12
chinsoon12

Reputation: 25225

You need to use with=FALSE as follows:

df[,c(2,1,3:ncol(df)),with=FALSE]

From ?data.table, under the Arguments for with

When j is a character vector of column names, a numeric vector of column positions to select or of the form startcol:endcol, and the value returned is always a data.table. with=FALSE is not necessary anymore to select columns dynamically. Note that x[, cols] is equivalent to x[, ..cols] and to x[, cols, with=FALSE] and to x[, .SD, .SDcols=cols].

Since c(2,1,3:100L) is a numeric column, then with=FALSE is not required and the columns are automatically returned. When it is c(2,1,3:ncol(df)), this expression will be evaluated and returned as a vector.

Should have a dupe somewhere

Upvotes: 1

Related Questions