18

Often, especially when asking questions on Stack Overflow, I would like to create a data table with dummy values. However, I'm not sure how to create a data table which is either empty or has dummy values. How do I do this?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
verybadatthis
  • 1,448
  • 2
  • 14
  • 32

1 Answers1

34

To make an empty data table, use:

DT <- data.table(
variable1 = integer(),
variable2 = character(),
variable3 = numeric()
)

To make a data table with fake data, use:

DT <- data.table(
variable1 = 1:5,
variable2 = c(1,2,5,6,8),
variable3 = c("a","b","c","d","e")
)
verybadatthis
  • 1,448
  • 2
  • 14
  • 32
  • 5
    Also worth to note a NULL data.table `data.table(NULL)` which is different from empty data.table. – jangorecki Apr 18 '15 at 00:29
  • 3
    Also see [how to create a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for plenty of other tips on creating sample data. You usually can easily swap `data.table()` for `data.frame()` if the problem is specific to that package. – MrFlick Apr 18 '15 at 04:09
  • 1
    Also may be worthwhile to take heed from the [`data.table` intro vignette](http://cran.r-project.org/web/packages/data.table/vignettes/datatable-intro.pdf) for more base examples good for demonstrating grouping – MichaelChirico Apr 21 '15 at 14:08
  • how do you specify the number of rows? – Herman Toothrot Dec 15 '16 at 16:27
  • for fake data? It just depends on the number of items you enter into each variable. e.g. in the example above variable1 = 1:5, or 1,2,3,4,5, then it would be 5 rows. If you set it to something else like variable1 = 1:10, then it would be 10 rows. For the empty data table it won't have any rows. does that help? – verybadatthis Dec 26 '16 at 09:00
  • 4
    Another solution is `setNames(data.table(matrix(nrow = 0, ncol = 3)), c("va", "vb", "vc"))` - see https://stackoverflow.com/questions/37376398/how-to-create-an-empty-datatable-with-columns-names-and-then-append-datatables-t/37376507 – Stefan Feb 06 '18 at 12:44
  • would be worthwhile to initialize a date format here as well – Kots Oct 01 '19 at 09:45
  • `variable4 = as.Date(integer(), origin = '1970-01-01')` for dates. – vladli Jan 03 '20 at 10:09