How to Convert Posix Date to Day of Year

How do you convert POSIX date to day of year?

An alternative is to format the "POSIXt" object using strftime():

R> today <- Sys.time()
R> today
[1] "2012-10-19 19:12:04 BST"
R> doy <- strftime(today, format = "%j")
R> doy
[1] "293"
R> as.numeric(doy)
[1] 293

which is preferable to remembering that the day of the years is zero-based in the POSIX standard.

R convert date to day of the year

This works for me

S = c("28/05/2016 07:00", "29/05/2016 07:00", "30/05/2016 07:00")
s_1 <- as.Date(S,format='%d/%m/%Y')
Day_S <- lubridate::yday(s_1)

Convert Date String in Format Mon Day, Year Time am/pm to POSIXlt format in R?

You will want to use the format "%b %d, %Y %I:%M %p" in as.POSIXlt()

  • %b for the abbreviated month (in the current locale)
  • %d for the day
  • %Y for the full four-digit year
  • %I for the hour (when using %p a.k.a am/pm)
  • %M for the minutes
  • %p for am/pm

So for a POSIXlt date-time, you can do

(x <- as.POSIXlt("Aug 19, 2015 07:09 am", format = "%b %d, %Y %I:%M %p"))
# [1] "2015-08-19 07:09:00 PDT"

As mentioned in the comments, you must have created the entire POSIX object in order to extract its parts (well, to formally extract them anyway).

months(x)
# [1] "August"
months(x, abbreviate = TRUE)
# [1] "Aug"

Additionally, in order to use strptime(), you must plan on creating the entire date-time object as you cannot just create a month and have it classed as such. See help(strptime) and help(as.POSIXlt) for more.

Generating a day of year column from date column in R?

Hi you can use the lubridate- Package

> library(lubridate)
> yday(as.POSIXct("2020-11-16"))
[1] 321

of course this also works with a vector/ column:

v<-paste0("2020-11-",1:30)
yday(as.POSIXct(v))


How to convert in both directions between year,month,day and dates in R?

Because there are so many ways in which a date can be passed in from files, databases etc and for the reason you mention of just being written in different orders or with different separators, representing the inputted date as a character string is a convenient and useful solution. R doesn't hold the actual dates as strings and you don't need to process them as strings to work with them.

Internally R is using the operating system to do these things in a standard way. You don't need to manipulate strings at all - just perhaps convert some things from character to their numerical equivalent. For example, it is quite easy to wrap up both operations (forwards and backwards) in simple functions you can deploy.

toDate <- function(year, month, day) {
ISOdate(year, month, day)
}

toNumerics <- function(Date) {
stopifnot(inherits(Date, c("Date", "POSIXt")))
day <- as.numeric(strftime(Date, format = "%d"))
month <- as.numeric(strftime(Date, format = "%m"))
year <- as.numeric(strftime(Date, format = "%Y"))
list(year = year, month = month, day = day)
}

I forego the a single call to strptime() and subsequent splitting on a separation character because you don't like that kind of manipulation.

> toDate(2004, 12, 21)
[1] "2004-12-21 12:00:00 GMT"
> toNumerics(toDate(2004, 12, 21))
$year
[1] 2004

$month
[1] 12

$day
[1] 21

Internally R's datetime code works well and is well tested and robust if a bit complex in places because of timezone issues etc. I find the idiom used in toNumerics() more intuitive than having a date time as a list and remembering which elements are 0-based. Building on the functionality provided would seem easier than trying to avoid string conversions etc.

Extract year from date

if all your dates are the same width, you can put the dates in a vector and use substring

Date
a <- c("01/01/2009", "01/01/2010" , "01/01/2011")
substring(a,7,10) #This takes string and only keeps the characters beginning in position 7 to position 10

output

[1] "2009" "2010" "2011"

extract day number of year from dates

you can use yday function from lubridate since you're already using it:

> yday(my.dates)
# [1] 3 28 12 33 63

Convert day of year to date

I found that @TMS answer ignores the year specified in the origin, replacing it with the actual year you are executing the command (as @Shekeine found). Doing this seems to work fine though:

as.Date(104, origin = "2014-01-01")

CAUTION: day-of-year is zero based in this case!

Date conversion from POSIXct to Date in R

The problem here is timezones - you can see you're in "HKT". Try:

as.Date(as.POSIXct("2013-01-01 07:00", 'GMT'))
[1] "2013-01-01"

From ?as.Date():

["POSIXct" is] converted to days by ignoring the time after midnight
in the representation of the time in specified timezone, default UTC



Related Topics



Leave a reply



Submit