14

I have a problem when I import a csv file with R:

example lines to import:

2010-07-27;91
2010-07-26;93
2010-07-23;88

I use the statement:

data <- read.csv2(file="...", sep=";", dec=".", header=FALSE)

when I try to aggregate this data with other ones originated by statistical analysis using cbind, the date is showed as an integer number because it was imported as factor.

If I try to show it as a string using as.character, the numerical data are transformed into characters too so they are unusable for statistical procedures.

lmo
  • 35,764
  • 9
  • 49
  • 57
keanu
  • 141
  • 1
  • 1
  • 3

3 Answers3

26

Use colClasses argument:

data <- read.csv2(file="...", sep=";", dec=".", header=FALSE,
     colClasses=c("Date",NA))

NA means "proceed as default"

After import you could convert factor to Date by

data[[1]] <- as.Date(data[[1]])
Marek
  • 45,585
  • 13
  • 89
  • 116
9

Perhaps you want to convert the character values to meaningful time values. In that case POSIXt time objects are a good choice.

Given your data file I'd do something like.

data <- read.table(file="...", sep = ";", as.is = TRUE)
data[,1] <- strptime(data[,1], "%Y-%m-%d")

Look up strptime in help for more details.

NOTE: If you're going to specify all the properties of the file just use read.table. The only purpose for all of the other read.xxx versions is to simplify the expression because the defaults are set. Here you used read.csv2 because it defaults to sep = ';'. Therefore, don't specify it again. Not having to specify that is the entire reason the command exists. Personally, I only use read.table because I can never remember the names/defaults of all the variants. In your case it's also the briefest call because it satisfies your header and dec defaults.

Andre Holzner
  • 16,980
  • 6
  • 47
  • 61
John
  • 22,043
  • 5
  • 50
  • 80
  • 1
    when I run your second line, I get an error message like 'provided 9 variables to replace 1 variables'. However, when I wrap `strptime` in `as.POSIXct(...)`, i.e. do `as.POSIXct(strptime(data[,1], "%Y-%m-%d") ` it seems to work. – Andre Holzner Jan 29 '12 at 21:34
  • If data[,1] is a vector of character strings of the format c("2010-07-23",...) what I wrote works. What does "seem to work" mean anyway? it sounds like you're just converting the default POSIXlt result and you have some other function not mentioned here using a POSIXlt value. That's fine, it's just a different result. That doesn't mean that what's there doesn't work. POSIXlt and POSIXct are of class POSIXt (which is the one I mentioned). – John Jan 30 '12 at 00:37
  • 1
    This is precisely what *`read.csv(...colClasses...)`* is for! See Marek's answer. Doing the conversion at read-time with colClasses is better than this answer because it doesn't waste lots of temporary memory. (Just do *`gc(reset=TRUE)`* afterwards.) Also, it's way more legible. – smci Jul 12 '12 at 16:09
  • I don't know about 'lots' of temporary memory, and I use colClasses from time to time. But sometimes you want to work with multiple classes. Furthermore, if you just want to change a few columns out of a bunch and they're in odd locations it requires you to create vectors for column classes that either require more code or counting a whole bunch of entries to make sure you hit the right one. AND, read.table() has the colClasses argument of course. – John Jul 12 '12 at 16:21
  • `strptime()` has the additional benefit of being able to read hours and minutes, as in `strptime(df[[1]], "%Y-%m-%d %H:%M")`, if so desired. – fbmd Dec 12 '12 at 12:24
7

Add as.is=TRUE to the read.csv call.

mbq
  • 18,025
  • 6
  • 45
  • 70