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?
Asked
Active
Viewed 3.8k times
18
-
Fyi, you shouldn't put the tags in the title like that. – Frank Apr 18 '15 at 01:27
-
Thanks for the heads up! I saw it once and didn't realize, but that definitely makes sense. – verybadatthis Apr 18 '15 at 07:43
1 Answers
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
-
5Also worth to note a NULL data.table `data.table(NULL)` which is different from empty data.table. – jangorecki Apr 18 '15 at 00:29
-
3Also 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
-
1Also 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
-
-
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
-
4Another 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
-
-