Serdar
Serdar

Reputation: 87

as.Date function doesn't convert date

I have a small problem with the as.Date function. I loaded a prn data as csv file, and my teacher said it was okay, then when I try to set my dates in the standard way (y,m,d) it gives me as a year a crazy number. PZU file is the stock prices for polish company. Also, I am not really sure how to set time also. I would really appreciate the help. Just for general info I am using R software with R studio interface.

pzu<-read.csv("PZU.prn",header=F)[,1:7]
names(pzu)<-c("name","date","time","open","high","low","close")
head(pzu)
#  name     date time open high low close
#1  PZU 20100512  845  349  349 349   349
#2  PZU 20100512  845  349  349 349   349
#3  PZU 20100512  845  349  349 349   349
#4  PZU 20100512  845  349  349 349   349
#5  PZU 20100512  845  349  349 349   349
#6  PZU 20100512  845  349  349 349   349

class(pzu$date) # output is an "integer"

str(pzu)
#data.frame':   960638 obs. of  7 variables:
# $ name : Factor w/ 1 level "PZU": 1 1 1 1 1 1 1 1 1 1 ...
# $ date : int  20100512 20100512 20100512 20100512 20100512 20100512 20100512 20100512 20100512 20100512 ...
# $ time : int  845 845 845 845 845 845 845 845 845 845 ...
# $ open : num  349 349 349 349 349 349 349 349 349 349 ...
# $ high : num  349 349 349 349 349 349 349 349 349 349 ...
# $ low  : num  349 349 349 349 349 349 349 349 349 349 ...
# $ close: num  349 349 349 349 349 349 349 349 349 349 ...

pzu$date<-as.Date(pzu$date)
head(pzu)
#name       date time open high low close
#1  PZU 7003-05-03  845  349  349 349   349
#2  PZU 7003-05-03  845  349  349 349   349
#3  PZU 7003-05-03  845  349  349 349   349
#4  PZU 7003-05-03  845  349  349 349   349
#5  PZU 7003-05-03  845  349  349 349   349
#6  PZU 7003-05-03  845  349  349 349   349

Upvotes: 6

Views: 31599

Answers (1)

Stephan Kolassa
Stephan Kolassa

Reputation: 8325

Your dates are not in a format as.Date() recognizes. First, convert them to character using as.character(), then specify the correct format via the format parameter to as.Date():

as.Date(as.character(20100512),format="%Y%m%d")

Alternatively, you could add in the time variable by converting to POSIXct. Convert the date as above. Take the hundreds in the time variable, multiply by 3600 (3600 seconds to an hour) and add. Take the remainder, multiply by 60 (60 seconds to a minute) and add again:

date.num <- 20100512
time.num <- 845
as.POSIXct(as.character(date.num),format="%Y%m%d") +
(time.num%/%100)*3600 +
(time.num%%100)*60

"2010-05-12 08:45:00 CEST"

Look at ?POSIXct for all the fun things R can do with times and dates.

Upvotes: 8

Related Questions