[R] Data() and CSV files

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Apr 27 19:13:12 CEST 2005


"McGehee, Robert" <Robert.McGehee at geodecapital.com> writes:

> Hello,
> 
> For reasons I don't understand, data() imports CSV (Comma-Separated
> Values) as if they were delimited by semicolons instead of commas. (Are
> semicolon-separated Comma-Separated-Value files common somewhere?) Given
> that this is the case, if I choose to put comma-delimited CSV files in
> my data directory, what is the preferred method of loading these into
> memory?

Semicolon-separated CSV's are common in various parts of Europe, but
usually coincident with comma as decimal separator (cf.
read.csv/read.csv2). I can't remember whether there was a reason for
the choice in data().
 
> data("filename")
> would be nice, but not applicable given the above conversion issue.
> 
> So, this was the best I came up with:
> 
> read.csv(file.path(.find.package("pkg"), "data", paste("filename",
> "csv", sep = ".")))
> 
> However, given that others undoubtedly like to include (non-semicolon)
> .csv files in their packages and load them easily, I would like to know
> if there is a more elegant way to load these files. Perhaps an
> annotation in the data/00Index or /data/datalist file that I am unaware
> of?

One trick is that .R files are processed before other files, so a mydata.R
file containing  mydata <- read.csv("mydata.csv") should do the trick.

It messes with lazy loading of data sets though.

An alternative is to preprocess the data set to (say) .Rda format.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list