Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

Converting date in Year.decimal form in R

I have a ts column that saves years in decimal format ie

1988.0
1988.25
1988.5
1988.75

and so on. I need to export it to a .csv file with the date in a "DD-MM-YYYY" format.

How do I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The lubridate package has a function, date_decimal that you can use for this.

x <- c(1988.0, 1988.25, 1988.5, 1988.75)
library(lubridate)
(f <- format(date_decimal(x), "%d-%m-%Y"))
# [1] "01-01-1988" "01-04-1988" "02-07-1988" "01-10-1988"

Then you can write it to a csv with

write.csv(f, "afilename.csv")   ## or write.table()

You'll probably want to check the output first and adjust some of the arguments to whatever format you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...