Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I initialize an empty or example data table?

Tags:

r

data.table

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?

like image 259
verybadatthis Avatar asked Apr 18 '15 00:04

verybadatthis


People also ask

How do you create an empty data table?

function clear() Simply remove all rows of data from the table.

How do I create an empty data table in R?

An empty data frame can also be created with or without specifying the column names and column types to the data values contained within it. data. frame() method can be used to create a data frame, and we can assign the column with the empty vectors.

How do I start a table in R?

We can create a table by using as. table() function, first we create a table using matrix and then assign it to this method to get the table format. Example: In this example, we will create a matrix and assign it to a table in the R language.


1 Answers

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")
)
like image 81
verybadatthis Avatar answered Oct 02 '22 13:10

verybadatthis