Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

as.Date function doesn't convert date

Tags:

r

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
like image 925
Serdar Avatar asked Apr 02 '14 13:04

Serdar


People also ask

What does the AS date function do in R?

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.”

What does the AS date function do?

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.

What is the default format for date in as date ()?

By default, the date format is MM/DD/YYYY HH24:MI:SS.US.

How do you convert a date?

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.


1 Answers

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.

like image 171
Stephan Kolassa Avatar answered Sep 22 '22 04:09

Stephan Kolassa