r - Why is as.Date is being returned as type 'double'? -


i'm having trouble working as.date function in r. have vector of dates i'm reading in .csv file coming in factor of integers or character (depending on how read in file, doesn't seem have issue), formatted %m/%d/%y.

i'm going through file row row, pulling out date field , trying convert use elsewhere using following code:

tmpdtm<-as.date(as.character(tempdf$mydate), "%m/%d/%y") 

this seems give me want, example, if starting value of 12/30/2014, value "2014-12-30" returned. however, if examine value using typeof(), r tells me data type 'double'. additionally, if try bind other values , store in data frame using c() or cbind(), in data frame, winds being stored 16434, looks me sort of different internal storage value of date. i'm pretty sure that's because if try convert value again using as.date(), throws error asking origin.

so, 2 questions: expected? if so, there more appropriate way convert date end date-typed object?

thank you

dates internally represented double, can see in following example:

> typeof(as.date("09/12/16", "%m/%d/%y")) [1] "double" 

it still marked class date, in

> class(as.date("09/12/16", "%m/%d/%y")) [1] "date" 

and because double, can computations it. because of class date, these computations lead dates:

> as.date("09/12/16", "%m/%d/%y") + 1 [1] "2016-09-13" > as.date("09/12/16", "%m/%d/%y") + 31 [1] "2016-10-13" 

edit have asked c() , cbind(), because can assciated strange behaviour. see following example, switching order within c changes not type class of result:

> c(as.date("09/12/16", "%m/%d/%y"), 1) [1] "2016-09-12" "1970-01-02" > c(1, as.date("09/12/16", "%m/%d/%y")) [1]     1 17056  > class(c(as.date("09/12/16", "%m/%d/%y"), 1)) [1] "date" > class(c(1, as.date("09/12/16", "%m/%d/%y"))) [1] "numeric" 

edit 2 - c() , cbind force objects of 1 type. first edit shows anomaly of coercion, generally, vector must of 1 shared type. cbind shares behavior because coerces matrix, in turn coerces single type.

for more on typeof , class see this link


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -