importDT {CALIBERdatamanage} | R Documentation |
Imports a text or Stata file to data.table, automatically converting dates
importDT(filename, datecolnames = NULL, verbose = TRUE, sep = NULL, zipname = NULL, key = NULL, convertLogical = TRUE, fread = TRUE, nrows = NULL, ...)
filename |
a single filename or path to a text file, Stata file, gz compressed text file or a zip file containing one file. |
datecolnames |
an argument passed to |
verbose |
whether to print information about the file loaded |
sep |
a single delimiter character. If NULL, the function will try to detect it (, ; or TAB) from the first line. |
zipname |
path to a zip file. This is only required if the zip file
contains more than one file, otherwise the path to the zip file
can be supplied as the |
key |
a character vector of key column names, to set the data.table key |
convertLogical |
whether to convert 1 / 0 columns to logical |
fread |
whether to use the new |
nrows |
number of rows to import, default (NULL) is to import all rows. |
... |
other arguments to pass to |
A convenience function for importing a text file, including handling zip compressed files, automatically detecting the delimiter and converting dates.
A data.table containing the imported data.
In the future this function may be updated to take advantage of the new fread function in the data.table package.
Anoop Shah
# Import a test dataset data(test_data) tempfile <- paste(tempdir(), '/tmp.csv', sep = '') write.csv(test_data, tempfile, row.names = FALSE) # Default (fread = TRUE) importDT(tempfile) # Using the older read.delim function importDT(tempfile, fread = FALSE) # Limiting the number of rows importDT(tempfile, nrows = 3) # Convert to FFDF as.ffdf(importDT(tempfile)) # Importing another test dataset data(test_import) write.csv(test_import, tempfile, row.names = FALSE) importDT(tempfile) # datenotok is not converted because of an error str(importDT(tempfile)) # Importing only one row (nrows is an argument to read.delim) importDT(tempfile, nrows = 1) unlink(tempfile) # Importing Stata file tempstata <- paste(tempdir(), '/tmp.dta', sep = '') write.dta(test_data, tempstata) importDT(tempstata) unlink(tempstata)