-5

example R dataframe:

n = c(2, 3, 5) 
s = c("aa", "bb", "cc") 
b = c(TRUE, FALSE, TRUE) 
df = data.frame(n, s, b)  

I can easily subset columns in a dataframe like this:

df.smaller <- df[c(1,2)]

  n  s
1 2 aa
2 3 bb
3 5 cc

Very handy!

However, with datatable (and I thought it is easier with datatable) I have not found such a quick way to do the same. How can I quick and easy do the same to a datatable?

dt = data.table(df)
dt.smaller <- dt[c(1,2)]

   n  s     b
1: 2 aa  TRUE
2: 3 bb FALSE

will return me the first two rows. Probably it is just a comma or something I have to change, but I can't figure it out.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Annamarie
  • 183
  • 1
  • 2
  • 13
  • 6
    [First result](http://stackoverflow.com/questions/28094645/select-subset-of-columns-in-data-table-r) from google search... – Sotos Jul 22 '16 at 08:25
  • I saw this too. But I thought there must be yet an easier way such as df[c(1,2)]. If there is not, fair enough. – Annamarie Jul 22 '16 at 08:32
  • 2
    Please read the [vignettes](https://github.com/Rdatatable/data.table/wiki/Getting-started). – Arun Jul 22 '16 at 08:34

1 Answers1

2

We need to use with = FALSE

dt[, 1:2, with = FALSE]

This is explained in the ?data.table

with: By default with=TRUE and j is evaluated within the frame of x; column names can be used as variables.

When with=FALSE 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 often useful in data.table to select columns dynamically

akrun
  • 874,273
  • 37
  • 540
  • 662