ColClasses date and time read.csv

I have some form data:

date,time,val1,val2
20090503,0:05:12,107.25,1
20090503,0:05:17,108.25,20
20090503,0:07:45,110.25,5
20090503,0:07:56,106.25,5

which comes from the csv file. I'm relatively new to R, so I tried

data <-read.csv("sample.csv", header = TRUE, sep = ",")

and using POSIXltas well as POSIXctin the argument colClasses, but I don't seem to be able to create a single column or “variable” from my date and time data. I want to do this, so I can choose arbitrary time frames for calculating current statistics, such as max, min, mean (and then boxplots, etc.).

I also thought that I could convert it to a time series and go around it in such a way

dataTS <-ts(data) 

but so far I have managed to use the beginning, end and frequency. Thank you for your help.

+3
source share
1 answer

R colClasses, "" CSV. date time POSIXlt:

dat <- read.csv(textConnection("date,time,val1,val2
                               20090503,0:05:12,107.25,1
                               20090503,0:05:17,108.25,20
                               20090503,0:07:45,110.25,5
                               20090503,0:07:56,106.25,5"))
dat <- within(dat, Datetime <- as.POSIXlt(paste(date, time),
                                          format = "%Y%m%d %H:%M:%S"))

[ , , "%Y%d%m %H:%M:%S"]

:

> head(dat)
      date    time   val1 val2            Datetime
1 20090503 0:05:12 107.25    1 2009-05-03 00:05:12
2 20090503 0:05:17 108.25   20 2009-05-03 00:05:17
3 20090503 0:07:45 110.25    5 2009-05-03 00:07:45
4 20090503 0:07:56 106.25    5 2009-05-03 00:07:56
> str(dat)
'data.frame':   4 obs. of  5 variables:
 $ date    : int  20090503 20090503 20090503 20090503
 $ time    : Factor w/ 4 levels "0:05:12","0:05:17",..: 1 2 3 4
 $ val1    : num  107 108 110 106
 $ val2    : int  1 20 5 5
 $ Datetime: POSIXlt, format: "2009-05-03 00:05:12" "2009-05-03 00:05:17" ...

date `:

> dat <- dat[, -(1:2)]
> head(dat)
    val1 val2            Datetime
1 107.25    1 2009-05-03 00:05:12
2 108.25   20 2009-05-03 00:05:17
3 110.25    5 2009-05-03 00:07:45
4 106.25    5 2009-05-03 00:07:56
+8

All Articles