<div dir="ltr">Perhaps smth like this:<div><br></div><div>nameToClass = data.table(name = c('Date', 'Numeric', 'Character'), class = list(Sys.Date()[0], numeric(0), character(0)), key = 'name')<br></div><div><br></div><div>emptyDT = function(names, types) nameToClass[types, setnames(as.data.table(class), names)]</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jul 23, 2015 at 2:01 PM, statquant3 <span dir="ltr"><<a href="mailto:statquant@outlook.com" target="_blank">statquant@outlook.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">My convoluted way<br>
<br>
#@usage: create empty vector<br>
newVec <- function(char){<br>
        if(char=='D') return(as.Date(integer(0)))<br>
        if(char=='N') return(numeric(0))<br>
        if(char=='C') return(character(0))<br>
        if(char=='I') return(integer(0))<br>
        return(numeric(0))<br>
}<br>
<br>
#@usage: create empty data.table with given types<br>
#@example:<br>
#       colNames <- c('col1','col2','col3'); colTypes <- 'DNC'; DT <-<br>
emptyDT(colNames,colTypes)<br>
emptyDT <- function(colNames,colTypes){<br>
        DT <- data.table(X=newVec(substr(colTypes,1L,1L)))<br>
        setnames(DT,'X',colNames[1L])<br>
        N <- length(colNames)<br>
        if(N>1){<br>
                for(i in 2:N){<br>
                        DT[,X:=newVec(substr(colTypes,i,i))]<br>
                        setnames(DT,'X',colNames[i])<br>
                }<br>
        }<br>
        return(DT)<br>
}<br>
<br>
R) colNames <- c('col1','col2','col3'); colTypes <- 'DNC'; DT <-<br>
emptyDT(colNames,colTypes)<br>
R) str(DT)<br>
Classes 'data.table' and 'data.frame':  0 obs. of  3 variables:<br>
 $ col1:Class 'Date'  num(0)<br>
 $ col2: num<br>
 $ col3: chr<br>
 - attr(*, ".internal.selfref")=<externalptr><br>
<br>
<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://r.789695.n4.nabble.com/create-empty-data-table-tp4710237p4710263.html" rel="noreferrer" target="_blank">http://r.789695.n4.nabble.com/create-empty-data-table-tp4710237p4710263.html</a><br>
<span class="">Sent from the datatable-help mailing list archive at Nabble.com.<br>
_______________________________________________<br>
datatable-help mailing list<br>
</span><a href="mailto:datatable-help@lists.r-forge.r-project.org">datatable-help@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a><br>
</blockquote></div><br></div>