I have a long csv file and I want to import some of the data (based on defining colClasses
) plus its corresponding timestamp. I tried this with two different methods, the first one with my own function (based on this answer). Here some basic input to reproduce the results:
setClass("myDate")
setAs("myDate", function(from) as.Date(from, format="%d.%m.%Y %H:%M:%S") )
data <- c("15.08.2008 00:00:00,Vienna,bla,142", "23.05.2010 01:00:00,Paris,bla,92")
con <- textConnection(data)
readout <- read.csv(con, colClasses=c('myDate', 'character', 'NULL', 'numeric'), header=FALSE)
print(readout)
However, the output contains only the date, not the time (readout$V1: Date, format: "2008-08-15" "2010-05-23"
):
V1 V2 V4
1 2008-08-15 Vienna 142
2 2010-05-23 Paris 92
I tried this also with a zoo
series, but I think this is not what I want, although this way it contains also the time (the data is indexed by the corresponding timestamp):
library(zoo)
csv <-
"timestamp,city,foo,elev
15.08.2008 00:00:00,Vienna,bla,142
23.05.2010 01:00:00,Paris,bla,92"
readout = read.zoo(text = csv, sep = ",", header = TRUE, index = "timestamp", format = "%d.%m.%Y %H:%M:%S", tz = "CET")
print(readout)
Which yields:
city foo elev
2008-08-15 00:00:00 Vienna bla 142
2010-05-23 01:00:00 Paris bla 92
What I actually want is the result from my own function but also containing the time, not just the date.