As.Date function does not convert date

I have a little problem with the function as.Date. I downloaded the prn data as a csv file, and my teacher said that everything is fine, then when I try to set my dates in a standard way (y, m, d), it gives me a year as an insane number. The PZU file is the stock price for a Polish company. Also, I'm not quite sure how to set the time. I am very grateful for the help. Just for general information, I use R software with the 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
+4
source share
1 answer

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

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

, POSIXct. , . , 3600 (3600 ) . , 60 (60 ) :

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"

?POSIXct , R .

+5

Source: https://habr.com/ru/post/1534649/


All Articles