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
The as. This function allows us to create a date value (without time) in R programming. It allows the various input formats of the date value as well through the format = argument. Remember that this function supports the standard date format as “YYYY-MM-DD.”
The as. Date() is a built-in R function that converts between character representations and class “Date” objects representing the calendar dates. Dates are represented as the number of days since 1970-01-01, with negative values for earlier dates.
By default, the date format is MM/DD/YYYY HH24:MI:SS.US.
Follow these steps: Select a blank cell and verify that its number format is General. Click the cell that contains the text-formatted date that you want to convert. Press ENTER, and the DATEVALUE function returns the serial number of the date that is represented by the text date.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With