Tobi1990
Tobi1990

Reputation: 123

Convert numeric values of a data table to dates

I want to convert a data table containing numeric values for 305 variables and 361 observations into a data table of same size containing dates. The data table does contain "NA"s.

The numeric value of the dates has the origin of excel. This is what I tried so far:

Rep_Day_monthly <- as.data.table(sapply(Rep_Day_monthly,as.numeric))
Rep_Day_monthly <- sapply(Rep_Day_monthly,as.Date)

Problem with this is, that the data table still contains numeric values, so e.g. 5963 instead of 1986-04-30.

Looking very much forward to your help!

Cheers

Upvotes: 0

Views: 649

Answers (1)

Andrew Gustar
Andrew Gustar

Reputation: 18425

as.Date needs an origin (i.e. a date corresponding to 0). If the data is from Excel, this will usually be 1 Jan 1970, so you could use Rep_Day_monthly <- as.data.table(lapply(Rep_Day_monthly,as.Date,origin="1970-01-01"))

Upvotes: 3

Related Questions