I am trying to convert an array of Julian time into the standard Gregorian calendar. For that, I am using chron, which seems quite simple. The problem is that the origin does not fit with the beginning of the time. That means, I know the date starts at 12:00:00 in 23 May 2014 and end at 5th Sep 2014 at 7am. Then, if I use chron as the following:
#The first 50 values
time <- dput(t[1:50])
structure(c(143.0208333, 143.0416667, 143.0625, 143.0833333,
143.1041667, 143.125, 143.1458333, 143.1666667, 143.1875, 143.2083333,
143.2291667, 143.25, 143.2708333, 143.2916667, 143.3125, 143.3333333,
143.3541667, 143.375, 143.3958333, 143.4166667, 143.4375, 143.4583333,
143.4791667, 143.5, 143.5208333, 143.5416667, 143.5625, 143.5833333,
143.6041667, 143.625, 143.6458333, 143.6666667, 143.6875, 143.7083333,
143.7291667, 143.75, 143.7708333, 143.7916667, 143.8125, 143.8333333,
143.8541667, 143.875, 143.8958333, 143.9166667, 143.9375, 143.9583333,
143.9791667, 144, 144.0208333, 144.0416667), .Dim = 50L)
#Convert into GCalen.
newtime <- chron(time, origin=c(month = 1, day = 1, year = 2014))
dates <- as.POSIXlt(newt, "GMT")
The dates start at:
"2014-05-24 00:29:59 GMT" "2014-05-24 01:00:00 GMT" "2014-05-24 01:30:00 GMT" "2014-05-24 01:59:59 GMT"....
Looking into the end of the dates, I would have the same problem, because it finishes at "2014-09-06 07:30:00 GMT", one day later ..so there is a substantial lapse that it needs to be fixed.
is there any way to add in the origin the time?? hour:min:sg??
Related
I was looking for someway to transform a date in POSIXct format to Julian day
_
The Julian Day Number (JDN) is the integer assigned to a whole solar day in the Julian day count starting from noon Universal time, with Julian day number 0 assigned to the day starting at noon on Monday, January 1, 4713 BC, proleptic Julian calendar (November 24, 4714 BC, in the proleptic Gregorian calendar)
_
But I just figured out how to make a year day - 1 to 365 for leap year.
Could someone help me find some function that turns POSIXct dates (like: 2010-10-02 21:00:00) into Julian dates?
I have a column on a dataframe with several dates to be transformed into Julian days.
head(head(all_jub2$timestamp_adjusted)
[1] 2010-10-02 21:00:00 2010-10-03 03:00:00 2010-10-03 09:00:00 2010-10-03 15:00:00
[5] 2010-10-03 21:00:00 2010-10-04 03:00:00
6120 Levels: 2003-10-17 21:00:00 2003-10-18 03:00:00 ... 2020-01-10 09:00:00
The lubridate package makes it easy to deal with dates. Does this solve your issue?
library(lubridate)
date <- as.POSIXct('2010-10-02 21:00:00')
julian <- yday(date)
You can do this with base R provided you start at the right point. The text in your question your data is still a factor-type variable. That is not good -- you need to parse this, and for example the anytime() function of the anytime package can help you. See other questions here on that.
Back to the question. If you start with a POSIXct variable from current time, and for argument's sake an hour ago to have vector, we can a) convert it to Date and then b) from Date to a Julian:
R> input <- Sys.time() + c(-3600,0)
R> input
[1] "2020-09-29 13:07:50.225898 CDT" "2020-09-29 14:07:50.225898 CDT"
R> as.Date(input)
[1] "2020-09-29" "2020-09-29"
R> julian(as.Date(input))
[1] 18534 18534
attr(,"origin")
[1] "1970-01-01"
R>
So for today our Julian date is 18534. For your first two data points, we get 14884 and 14885.
R> input <- c("2010-10-02 21:00:00", "2010-10-03 03:00:00")
R> anytime::anydate(input)
[1] "2010-10-02" "2010-10-03"
R> julian(anytime::anydate(input))
[1] 14884 14885
attr(,"tzone")
[1] "America/Chicago"
attr(,"origin")
[1] "1970-01-01"
R>
If you only want to day of the year, you get that as the component yday of the POSIXlt representation but you need to adjust by one as it is zero based:
R> as.POSIXlt(anytime::anytime(input))$yday + 1
[1] 275 276
R>
Thanks for the answers, but i foun what i've been looking for using the package insol
library(insol)
julian_day <- insol::JD(as.POSIXct('2010-10-02 21:00:00'))
[1] 2455473
day month year hour
01 05 2017 00
05 12 2017 01
10 07 2017 23
i don't have column minute and seconds now i am trying to get a single variable as date_time in the format of
2017-05-01 00:00:00
2017-12-05 01:00:00
2017-07-10 23:00:00
getting the error while using the below code
df$date <- as.Date(with(df, paste(year, month, day,hour,sep="-")),
"%Y-%m-%d %H:%M:%S")
thanks in advance
We can try paste the columns together with sprintf, then convert to datetime with as.POSIXct
as.POSIXct(do.call(sprintf, c(df1, fmt = c("%02d-%02d-%4d %02d"))), format = "%d-%m-%Y %H")
#[1] "2017-05-01 00:00:00 IST" "2017-12-05 01:00:00 IST" "2017-07-10 23:00:00 IST"
Or use lubridate
library(lubridate)
with(df1, ymd_h(paste(year, month, day, hour, sep= ' ')))
since year, month, day, hour are stored in different columns, why not just use ISOdate function? for instance:
> ISOdate("2017","12", "05", "01")
[1] "2017-12-05 01:00:00 GMT"
if you don't want to see time zone info, just wrap ISOdate with as.Date
> as.Date(ISOdate("2017","12", "05", "01"))
[1] "2017-12-05"
But this changes the class of object from POSIXct to Date and (correct me if I'm wrong) does not really remove time zone info.
This was discussed in another question How to convert in both directions between year,month,day and dates in R?
You can do this directly without making a string using lubridate.
library(lubridate)
df1 %>%
mutate(date = make_datetime(year, month, day, hour))
This is probably more reliable than using string parsing.
I create a time series dataframe that I will use to merge other time series data into.
dates2010 <- seq(as.POSIXct("2010-06-15 00:00:00", tz = "GMT"), as.POSIXct("2010-09-15 23:00:00", tz = "GMT"), by="hour") # make string of DateTimes for summer 2010
dates2011 <- seq(as.POSIXct("2011-06-15 00:00:00", tz = "GMT"), as.POSIXct("2011-09-15 23:00:00", tz = "GMT"), by="hour") # make string of DateTimes for summer 2011
dates <- c(dates2010, dates2011) # combine the dates from both years
sites <- c("a", "b", "c") # make string of all sites
datereps <- rep(dates, length(sites)) # repeat the date string the same number of times as there are sites
sitereps <- rep(sites, each = length(dates)) # repeat each site in the string the same number of times as there are dates
n <- data.frame(DateTime = datereps, SiteName = sitereps) # merge two strings with all dates and all sites
n <- n[order(n$SiteName, n$Date),] # re-order based on site, then date
If I run the above code, 'dates2010' and 'dates2011' are in GMT format: dates2010[1] "2011-06-15 00:00:00 GMT".
But when I create the object 'dates' for some reason format switches to EST: dates[1]
"2010-06-14 19:00:00 EST"
Maybe it has something to do with POSIX classes?
class(dates2010)
[1] "POSIXct" "POSIXt"
I attempted to change the default time zone for R to GMT to avoid time zone switching problems. This results in an NA coersion error when I attempt to order the data frame 'n' and merge other data frames into 'n'.
n <- n[order(n$SiteName, n$Date),]
Warning message:
In xtfrm.POSIXct(x) : NAs introduced by coercion
Any thoughts on how I might keep time zones constant and avoid the NA coercion errors? Thank You!
c() drops attributes. So when you created dates, the time zone was dropped and it automatically defaulted to the current locale. Fortunately you can use structure() and set the time zone there.
dates <- structure(c(dates2010, dates2011), tzone = "GMT")
head(dates)
# [1] "2010-06-15 00:00:00 GMT" "2010-06-15 01:00:00 GMT"
# [3] "2010-06-15 02:00:00 GMT" "2010-06-15 03:00:00 GMT"
# [5] "2010-06-15 04:00:00 GMT" "2010-06-15 05:00:00 GMT"
If dates was already created, you can add/change the tzone attribute later.
attr(dates, "tzone") <- "GMT"
I have some dates in POSIXct format
head(data$Dates,3)
[1] "2015-01-02 16:34:01 GMT" "2015-01-28 16:33:03 GMT" "2015-01-16 20:55:35 GMT"
And I would like to create a column that is the DATE of each element with the time of 21:00:00 GMT and then subtract this new date from the original.
So from that vector of dates I want to create a vector of POSIXct that looks like
"2015-01-02 21:00:00 GMT" "2015-01-28 21:00:00 GMT" "2015-01-16 21:00:00 GMT"
Then I want to subtract this new vector from the first one.
How do I create the vector that looks like:
"2015-01-02 21:00:00 GMT" "2015-01-28 21:00:00 GMT" "2015-01-16 21:00:00 GMT"
Thank you.
I recommend using the lubridate package. It will enable you to subtract dates from each other. Do you want to set the time to be the same thing for each one as an intermediary step in date subtraction? Slicing the first part of the timestamp to just get the date, using something like:
clean_date <- function(date){
date = substr(date, 1, 10)
return(date)
}
df$Date <- clean_date(df$Date)
and then just converting that to a lubridate date object, using
df$Date <- ymd(date)
should allow you to find the difference between the two dates easily, and will create a difftime object that will give you the the differenced between the two dates in your chosen unit.
It's not entirely clear if this is the type of result you are going after, but this should get you started.
dates <- as.POSIXct(c("2015-01-02 16:34:01 GMT",
"2015-01-28 16:33:03 GMT",
"2015-01-16 20:55:35 GMT"))
library(lubridate)
orig_dates <- ymd_hms(dates)
trunc_dates <- floor_date(orig_dates, "day")
trunc_dates_21st_hour <- trunc_dates + ehours(21)
orig_dates - trunc_dates_21st_hour
# Time differences in mins
# [1] -265.983333 -266.950000 -4.416667
I am working with csv timestamp data given in the form '%j%Y %H:%M with no leading zeroes. Here are some time stamp examples:
112005 22:00
1292005 6:00
R is reading the first line at the 112th day of the 005th year. How can I make R correctly parse this information?
Code I'm using which doesn't work:
train$TIMESTAMP <- strptime(train$TIMESTAMP, format='%j%Y %H:%M', tz='GMT')
train$hour <- as.numeric(format(train$TIMESTAMP, '%H'))
I don't think there's any simple way to decipher where the day stops and the year starts. Maybe you could split it at something that looks like a relevant year (20XX):
gsub("^(\\d{1,3})(20\\d{2})","\\1 \\2",train$TIMESTAMP)
#[1] "11 2005 22:00" "129 2005 6:00"
and do:
strptime(gsub("^(\\d{1,3})(20\\d{2})","\\1 \\2",train$TIMESTAMP), "%j %Y %H:%M")
#[1] "2005-01-11 22:00:00 EST" "2005-05-09 06:00:00 EST"