Paul Ledin
Paul Ledin

Reputation: 83

Returning a data table from a subsetted data table

Ok maybe I'm testing the bounds of the whole no-stupid-question philosophy but can somebody set me straight on how one subsets a data table's columns into another data table?

So let's say I create this data table below, and I'd like a new data table with only two of the three columns in it.

example <- data.table(c("Bob","May","Sue","Bob","Sue","Bob"), 
                      c("A","A","A","A","B","B"),  
                      as.Date(c("2010/01/01", "2010/01/01", "2010/01/01", 
                      "2012/01/01", "2012/01/11", "2014/01/01")))

I'm trying to make sense of section 1.3 and 1.4 in the datatable-faq(http://cran.r-project.org/web/packages/data.table/vignettes/datatable-faq.pdf) in that I what my result to be a new data table, not a character vector. According to the FAQ this is accomplished through the use of DT[,.()], however my attempts at that yield:

example[,.(V2,V3)]
Error in eval(expr, envir, enclos) : could not find function "."

Were I to guess, it looks to me like some kind of versioning issue, but I've got the latest version of R and that FAQ was updated like a week ago.

Out of curiosity and on a side note, anybody know why the default result of sub-setting a data table is anything other than another data table? I'm new to R(obviously) but to a programming guy this seems an awful lot like going out of your way just to be obtuse.

Upvotes: 1

Views: 107

Answers (1)

n8sty
n8sty

Reputation: 1448

Have you installed and then loaded the most recent version of the data.table package?

install.packages('data.table')
require(data.table)

example <- data.table(c("Bob","May","Sue","Bob","Sue","Bob"), 
                      c("A","A","A","A","B","B"),  
                      as.Date(c("2010/01/01", "2010/01/01", "2010/01/01", 
                                "2012/01/01", "2012/01/11", "2014/01/01")))


example[,.(V2,V3)]
#   V2         V3
#1:  A 2010-01-01
#2:  A 2010-01-01
#3:  A 2010-01-01
#4:  A 2012-01-01
#5:  B 2012-01-11
#6:  B 2014-01-01

I'm up to date with the most recent one from CRAN so I think you may just have an older version of the table. See Arun's comment.

Upvotes: 3

Related Questions