Extracting 2 digit hour from POSIXct in R - r

I would like to extract the hour from a POSIXct time in R, but retrieve the 2 digit answer.
For example,
test=as.POSIXct("2015-03-02 03:15:00")
test
[1] "2015-01-02 03:15:00 GMT"
month(testing)
[1] 1
hour(testing)
[1] 3
The results give the relevant month and hour, but I would like to see 01 and 03 instead of just 1 and 3.

Try to do this:
strftime(test, format="%H")
to extract hours and
strftime(test, format="%m")
for month

Related

R as.Date() convert year and week number to date

I'm running into trouble converting a year + month string containing week number 53 using the as.Date() function in R.
The code works for the top example for week number 52 but returns NA for the bottom example for week number 53.
a <- "2017521"
as.Date(a, '%Y%W%u')
"2017-12-25"
b <- "2017531"
as.Date(b, '%Y%W%u')
NA
You're getting NA for b <- "2017531" because you're trying to reference a date that did not exist.
This has to do with the way you formatted your date, and the way the calendar is initiated.
%W refers to the numerical week 00-53
%u refers to the day of the week 1-7 Monday is 1
b <- "2017531"
as.Date(b, '%Y%W%u')
# [1] NA
Week 53 day 1 would refer to Monday of the 53rd week. But the only day of the week that occurred on the 53rd week of 2017 was Sunday.
c <- "2017537"
as.Date(a, '%Y%W%u')
# [1] "2017-12-31"
You can further confirm this by checking the date Saturday of week 52:
d <- "2017526"
as.Date(a, '%Y%W%u')
# [1] "2017-12-30"

Getting weeks from timestamp which spans more than a year in R

I have a dataset containing a series of time stamps from 01/01/2015 to 01/01/2017 (dd/mm/yyyy). I want to convert it to Weeks (i.e) 01/01/2015 Week 0, 08/01/2015 becomes Week 1 ... 01/01/2017 should become Week 104 (or something around this number).
I tried the following method
> sD
"2016-04-13 05:30:00 IST" "2017-04-10 05:30:00 IST"
> format(as.Date(sD,format = guess_formats(sD,c('dmy'))), "%W")
"15" "15"
Here for the same date but for different years I am getting the same Week. I need the output to change with year also. How to go about doing this?
Just take the difference and specify the unit as weeks:
x <- as.Date(c("2015-01-01","2015-01-08","2017-01-01"))
difftime(x, as.Date("2015-01-01"), units="weeks")
#Time differences in weeks
#[1] 0.0000 1.0000 104.4286

Parsing Ambiguous Dates in R

I want to know how to find out which part of string is month and which part of string is day while parsing dates.
The problem is 01-06-2017 can be 1 June or it can be 6 January. How to parse it correctly. In India we write dates as Day Month Year mostly, in west it is Month Day Year mostly, when I have mixed data how do I impute which is the month and which is the day
because the data is not clean enough, it sometimes have dates in mdy and sometimes in dmy format and if the number is less than 12, it is difficult to know if it is a day or a month
11/1/11 can be 11 Jan 2011 or 1 November 2011
Example
I am using lubridate package and I have dates in this format
library(lubridate)
fundates2=c("1Apr2017","12-30-2017","1/6/17")
fun3=dmy(fundates2)
## Warning: 1 failed to parse.
fun3
## [1] "2017-04-01" NA "2017-06-01"
fun4=mdy(fundates2)
## Warning: 1 failed to parse.
fun4
## [1] NA "2017-12-30" "2017-01-06"
Well, you have yo know from your context which one is the correct.
To check which one your date is you can simply add 1 day to it:
In fun3:
fun3 + 1
[1] "2017-04-02" NA "2017-06-02"
You can see that the month is the 06.
In fun4:
fun4 + 1
[1] NA "2017-12-31" "2017-01-07"
You can see the month is 01

R-How to calculate year of birth given age and date of visit

I have a data set like this
Age Date of visit
28 2015-03-28 17:51:31 CET
26 2015-03-28 18:40:11 CET
how do I calculate the DOB based on the above information.
Try something with lubridate
library(lubridate)
d1 <- ymd_hms("2015-03-28 17:51:31 CET")
d1
[1] "2015-03-28 17:51:31 UTC"
d1 - years(28)
[1] "1987-03-28 17:51:31 UTC"
Not sure you want that precision with the time etc, but that can be removed

R Date time conversion

I am new to R and I have a data frame with date time as variable. For every hour each day temperature is recorded, and date time is in format of YYYY-MM-DD 00:00:00.
Now I would like to convert the time into a factor ranging from 0 to 23 each day.
So For each day my new column should have factors 0 to 23. Could anyone help me with this? My 2015-01-01 00:00:00, should give me 0, while 2015-01-01 01:00:00, should give me 1 and so on. Also my 2015-01-02 00:00:00 should be 0 again.
You can convert your timestamp into a POSIXlt object. Once you have that, you can obtain the hour directly like this:
> timestamp <- as.POSIXlt("2015-01-01 00:00:00")
> timestamp
[1] "2015-01-01 MYT"
> timestamp$hour
[1] 0
Using a sample data, one way would be the following.
mydf <- data.frame(id = c(1,1,1,2,2,1,1),
event = c("start", "valid", "end", "start", "bad", "start", "bad"),
time = as.POSIXct(c("2015-05-16 20:46:53", "2015-05-16 20:46:56", "2015-05-16 21:46:59",
"2015-05-16 22:46:53", "2015-05-16 22:47:00", "2015-05-16 22:49:05",
"2015-05-16 23:49:09"), format = "%Y-%m-%d %H:%M:%S"),
stringsAsFactors = FALSE)
library(dplyr)
mutate(mydf, group = factor(format(time, "%H")))
# id event time group
#1 1 start 2015-05-16 20:46:53 20
#2 1 valid 2015-05-16 20:46:56 20
#3 1 end 2015-05-16 21:46:59 21
#4 2 start 2015-05-16 22:46:53 22
#5 2 bad 2015-05-16 22:47:00 22
#6 1 start 2015-05-16 22:49:05 22
#7 1 bad 2015-05-16 23:49:09 23
Tim's answer using POSIXlt is probably the best option, but here's a regex way just in case:
> times <- c("2015-01-01 00:00:00", "2015-01-01 01:00:00", "2015-01-02 00:00:00")
> regmatches(times, regexpr("(?<=-\\d{2} )\\d{2}", times, perl=TRUE))
[1] "00" "01" "00"
With the extracted hours you can make them factors or integers as necessary.
#Sairam, in addition to #jazzurro's use of 'dplyr' (which, like jazzurro, many R-insitas routinely use)...in the future, if you need/want a simple & powerful way to manipulate dates, you're encouraged to gain familiarity with another package: 'lubridate.'
lubridate makes working with dates a snap. Hope this helps and best regards on your project.

Resources