[R] Help with read.csv
David Wolfskill
r at catwhisker.org
Thu Mar 10 00:10:51 CET 2011
On Wed, Mar 09, 2011 at 04:32:29PM -0600, Giovanni Petris wrote:
>
> Hello,
>
> I have a file that looks like this:
>
> Date,Hour,DA_DMD,DMD,DA_RTP,RTP,,
> 1/1/2006,1,3393.9,3412,76.65,105.04,,
> 1/1/2006,2,3173.3,3202,69.20,67.67,,
> 1/1/2006,3,3040.0,3051,69.20,77.67,,
> 1/1/2006,4,2998.2,2979,67.32,69.10,,
> 1/1/2006,5,3005.8,2958,65.20,68.34,,
>
> where the ',' is the separator and I tried to read it into R, but...
>
> > y <- read.csv("Data/Data_tmp.csv", header = FALSE, skip = 1,
> + colClasses = c("character", "int", rep("double", 4)),
> + col.names = c("Date","Hour","DA_DMD","DMD","DA_RTP", "RTP"),
> + flush = TRUE)
> Error in read.table(file = file, header = header, sep = sep, quote = quote, :
> more columns than column names
>
> count.fields() gives me 8 fields per line, so I tried other variations,
> like the following, with two fictitious extra fields, but...
>...
> Error in methods::as(data[[i]], colClasses[i]) :
> no method or default for coercing "character" to "int"
>
> Could anybody please tell me what I am doing wrong and how I could read
> my data into R?
I copied the data locally, then I read it using:
> y <- read.csv("Data_tmp.csv", header = TRUE, colClasses = c("character", "integer", rep("double", 4), "NULL", "NULL"))
which yields:
> y
Date Hour DA_DMD DMD DA_RTP RTP
1 1/1/2006 1 3393.9 3412 76.65 105.04
2 1/1/2006 2 3173.3 3202 69.20 67.67
3 1/1/2006 3 3040.0 3051 69.20 77.67
4 1/1/2006 4 2998.2 2979 67.32 69.10
5 1/1/2006 5 3005.8 2958 65.20 68.34
>
Is that what you had in mind?
Issues I tried to address:
* The class is "integer", not "int".
* I used "NULL" class to refer to columns that are to be skipped.
* I made use of the headers, rather than skipping them & re-coding their
content in the read.csv() invocation.
Peace,
david
--
David H. Wolfskill r at catwhisker.org
Depriving a girl or boy of an opportunity for education is evil.
See http://www.catwhisker.org/~david/publickey.gpg for my public key.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110309/255c2c2c/attachment.bin>
More information about the R-help
mailing list