Converting date in Year.decimal form in R
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.
Convert decimal month and year into Date
You may use some nice functions from the zoo
package:
as.yearmon
to convert year and floor
of the decimal month to class yearmon
.
Then use as.Date.yearmon
and its frac
argument to coerce the year-month to class Date
.
library(zoo)
df$date = as.Date(as.yearmon(paste(df$year, floor(df$decimal_month), sep = "-")),
frac = df$decimal_month - floor(df$decimal_month))
# decimal_month year date
# 1 4.75 2011 2011-04-22
# 2 5.00 2011 2011-05-01
# 3 5.25 2011 2011-05-08
If desired, day of year is simply format(df$date, "%j")
How to convert decimal date format (e.g. 2011.580) to normal date format?
Slightly different results with lubridate
:
library(lubridate)
decimals <- c(2001.667, 2004.083, 2008.750, 2011.583, 2011.917)
format(date_decimal(decimals), "%Y-%m-%d")
# [1] "2001-09-01" "2004-01-31" "2008-10-01" "2011-08-01" "2011-12-01"
How to calculate a decimal month in R in a particular year?
You can use monthDays
function from Hmisc package
> require(Hmisc)
> library(lubridate)
> month(dates) + day(dates)/monthDays(dates)
[1] 1.774194 4.266667 3.161290 3.258065 2.500000 1.903226 2.750000 3.903226 4.033333
[10] 2.500000 1.903226 1.612903 3.387097 1.967742 4.566667
Substract decimal years from date in r
We could use years
and months
v1 <- 5.5
yr <- as.integer(v1)
mth <- as.integer((v1* 12) %% 12)
ymd("2021-05-21") - (years(yr) + months(mth))
#[1] "2015-11-21"
Dealing with numeric (decimal) dates in R?
If you can use POSIXct, you can do for example
df$Opentime <- as.POSIXct( df$Opentime*24*60*60,
origin="1900-01-01",
tz="UTC")
Reasoning: POSIXct is just number of seconds since origin
How to find decimal representation of years in R?
It's very unclear what you're trying to do exactly here, which makes accuracy difficult to talk about.
lubridate
has a function decimal_date
which turns dates into decimals. But since 3 decimal places gives you 1000 possible positions within a year, when we only have 365/366 days, there are between 2 and 3 viable values that fall within a day. Accuracy depends on when in the day you want the result to fall.
> decimal_date(as.POSIXlt("2016-01-10 00:00:01"))
[1] 2016.025
> decimal_date(as.POSIXlt("2016-01-10 12:00:00"))
[1] 2016.026
> decimal_date(as.POSIXlt("2016-01-10 23:59:59"))
[1] 2016.027
In other words, going beyond 3 decimal places is only really important if you're interested in the time of day.
Related Topics
How to Delete the First Row of a Dataframe in R
Best Way to Transpose Data.Table
Remove Facet_Wrap Labels Completely
Outputting Multiple Lines of Text with Rendertext() in R Shiny
How to Perform Multiple Left Joins Using Dplyr in R
Purrr Map Equivalent of Nested for Loop
Write.CSV for Large Data.Table
Possible to Show Console Messages (Written with 'Message') in a Shiny Ui
Set R Plots X Axis to Show at Y=0
How to Change Xts to Data.Frame and Keep Index
Ggplot Geom_Bar: Meaning of Aes(Group = 1)
Specifying Formula in R with Glm Without Explicit Declaration of Each Covariate
How to Group Data.Table by Multiple Columns
Differencebetween Cat and Print
Data.Table Row-Wise Sum, Mean, Min, Max Like Dplyr
Combined Plot of Ggplot2 (Not in a Single Plot), Using Par() or Layout() Function