16

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.

3 Answers 3

30

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]])
Sign up to request clarification or add additional context in comments.

Comments

10

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.

5 Comments

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.
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).
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.
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.
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.
7

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.