How to extract the hour of this kind of timestamp? [duplicate] - r

This question already has an answer here:
Extracting 2 digit hour from POSIXct in R
(1 answer)
Closed 7 years ago.
This is how my timestamp looks like
date1 = timestamp[1]
date1
[1] 07.01.2016 11:52:35
3455 Levels: 01.02.2016 00:11:52 01.02.2016 00:23:35 01.000:30:21 31.01.2016 23:16:18
When I try to extract the hour, I get an invalid 'trim' argument. Thank you !
format(date1, "%I")
Error in format.default(structure(as.character(x), names = names(x), dim = dim(x), :
invalid 'trim' argument
How can I extract single components like the hour from this timestamp.

With base R:
format(strptime(x,format = '%d.%m.%Y %H:%M:%S'), "%H")
[1] "11"
data
x <- as.factor("07.01.2016 11:52:35")

You first need to parse the time
d = strptime(date1,format = '%d.%m.%Y %H:%M:%S')
Then use lubridate to extract parts like hour etc.
library(lubridate)
hour(d)
minute(d)

Related

Converting non-standard date format strings ("April-20") to date objects R

I have a vector of date strings in the form month_name-2_digit_year i.e.
a = rbind("April-21", "March-21", "February-21", "January-21")
I'm trying to convert that vector into a vector of date objects. I'm aware this question is very similar to this: Convert non-standard date format to date in R posted some years ago, but unfortunately, it has not answered my question.
I have tried the following as.Date() calls to do this, but it just returns a vector of NA. I.e.
b = as.Date(a, format = "%B-%y")
b = as.Date(a, format = "%B%y")
b = as.Date(a, "%B-%y")
b = as.Date(a, "%B%y")
I'm also attempted to do it using the convertToDate function from the openxlsx package:
b = convertToDate(a, format = "%B-%y")
I have also tried all the above but using a single character string rather than a vector, but that produced the same issue.
I'm a little lost as to why this isn't working, as this format has worked in reverse earlier in my script (that is, I had a date object already in dd-mm-yyyy format and converted it to month_name-yy using %B-%y). Is there another way to go from string to date when the string is a non-standard (anything other than dd-mm-yyy or mm-dd-yy if you're in the US) date format?
For the record my R locales are all UK and english.
Thanks in advance.
A Date must have all three of day, month and year. Convert to yearmon class which requires only month and year and then to Date as in (1) and (2) below or add the day as in (3).
(1) and (3) give first of month and (2) gives the end of the month.
(3) uses only functions from base R.
Also consider not converting to Date at all but just use yearmon objects instead since they directly represent a year and month which is what the input represents.
library(zoo)
# test input
a <- c("April-21", "March-21", "February-21", "January-21")
# 1
as.Date(as.yearmon(a, "%B-%y"))
## [1] "2021-04-01" "2021-03-01" "2021-02-01" "2021-01-01"
# 2
as.Date(as.yearmon(a, "%B-%y"), frac = 1)
## [1] "2021-04-30" "2021-03-31" "2021-02-28" "2021-01-31"
# 3
as.Date(paste(1, a), "%d %B-%y")
## [1] "2021-04-01" "2021-03-01" "2021-02-01" "2021-01-01"
In addition to zoo, which #G. Grothendieck mentioned, you can also use clock or lubridate.
clock supports a variable precision calendar type called year_month_day. In this case you'd want "month" precision, then you can set the day to whatever you'd like and convert back to Date.
library(clock)
x <- c("April-21", "March-21", "February-21", "January-21")
ymd <- year_month_day_parse(x, format = "%B-%y", precision = "month")
ymd
#> <year_month_day<month>[4]>
#> [1] "2021-04" "2021-03" "2021-02" "2021-01"
# First of month
as.Date(set_day(ymd, 1))
#> [1] "2021-04-01" "2021-03-01" "2021-02-01" "2021-01-01"
# End of month
as.Date(set_day(ymd, "last"))
#> [1] "2021-04-30" "2021-03-31" "2021-02-28" "2021-01-31"
The simplest solution may be to use lubridate::my(), which parses strings in the order of "month then year". That assumes that you want the first day of the month, which may or may not be correct for you.
library(lubridate)
x <- c("April-21", "March-21", "February-21", "January-21")
# Assumes first of month
my(x)
#> [1] "2021-04-01" "2021-03-01" "2021-02-01" "2021-01-01"

How to extract month from a column in R [duplicate]

This question already has answers here:
How to extract month from a Year-Month (2017-10) type object in R?
(3 answers)
Closed 1 year ago.
I am trying to create a column which returns the month of a given observation.
My dataframe is called:" FF5_Class"
I tried to run the following code:
FF5_class$Month <- FF5_class %>%
month(date)
However, i received the following error message:"
"Error in as.POSIXlt.default(x, tz = tz(x)) :
do not know how to convert 'x' to class “POSIXlt”
A snip from my data is like this:
date Month
2000-01
2000-02
2000-03
2000-04
2000-05
2000-06
2000-07
2000-08
2000-09
2000-10
2000-11
2000-12
2001-01
2001-02
2001-03
2001-04
2001-05
2001-06
2001-07
2001-08
2001-09
2001-10
2001-11
2001-12
How can I do this? Thank you very much.
We can use couple of ways to do this. Regex option would be to remove the characters until the - with sub on the 'date' column
FF5_class$Month <- sub(".*-", "", FF5_class$date))
Or convert to a Date class after pasteing the day, then use format
FF5_class$Month <- format(as.Date(paste0(FF5_class$date, '-01')), '%m')
month works only a Date object and not on a character class

Reformatting date and timestamp with r [duplicate]

This question already has answers here:
Changing date format in R
(7 answers)
Closed 3 years ago.
In order to prevent an error in uploading xls data into a sql database, I am trying to reformat a date type of "08/22/2019 02:05 PM CDT" and want only the date, not the time or the timezone. Many efforts to use the default, POSIX and lubridate actions have failed. The xls file formats the date column as general.
I have a column of data to convert, not a single cell. This is a part of a loop for multiple files in a folder.
Failures:
#mydata_r11_Date2 <- strptime(as.character(mydata_r11_Date$Date), "%d/%m/%Y")
# parse_date_time(x = mydata_r11_Date$Date,
# orders = c("d m y", "d B Y", "m/d/y"),
# locale = "eng")
#
#
# mydata_r11_Date <- as.character(mydata_r11_Date)
mydata_r11_Date <- gsub('^([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]+[+-][0-9]{2}):([0-9]{2})$',
'\\1\\2',
mydata_r11_Date$Date)
ymd_hms(mydata_r11_Date$Date)
mydata_r11_Date <- as_date (mydata_r11_Date$Date,format = "%Y-%m-%d")
mydata_r11_Date2 <- format(as.Date(mydata_r11_Date,"%Y-%m-%d"),"%Y-%m-%d")
Errors include:
Warning message:
All formats failed to parse. No formats found.
Error in as.Date.default(x, ...) :
do not know how to convert 'x' to class “Date”
Error in as.Date.default(mydata_r11_Date$Date, format = "%Y-%m-%d") :
do not know how to convert 'mydata_r11_Date$Date' to class “Date”
Error: unexpected ',' in " mydata_r11_Date <- as.Date(mydata_r11_Date$Date),"
Error in as_date(x) : object 'x' not found
library(readxl)library(reshape2) library(lubridate)
import xsl
mydata_r11 <- read_excel("C:/FOLDER/FOLDER/FOLDER/OUTPUT/WADUJONOKO_student_assessment_results.xls",1,skip = 1, col_types = "list")
Isolate date column
mydata_r11_Date <- mydata_r11[,c(8)]
Convert date
mydata_r11_Date 2 <-
Have "08/22/2019 02:05 PM CDT"
Want "08/22/2019"
I don't understand why you are resorting to complex regex here when you seem to only want the date component, which is the first 10 characters of the timestamps. Just take the substring and then call as.Date with an appropriate format mask:
x <- "08/22/2019 02:05 PM CDT"
y <- substr(x, 1, 10)
as.Date(y, format = "%m/%d/%Y")
[1] "2019-08-22"

String format parsing in R [duplicate]

This question already has answers here:
How to convert dd/mm/yy to yyyy-mm-dd in R
(6 answers)
Closed 3 years ago.
When I parse a character string into a date, Why does this not throw an error or an NA? I have tried the following
t <- "31-Oct-2012"
as.Date(t, format = "%d-%B-%Y") # this produces the expected result
as.Date(t, format = "%d-%B-%y") # I was expecting an NA
Instead I get
[1] "2020-10-31"
Because %y is for two digit year, so it takes only first two digits and ignores the rest. It treats t as
as.Date("31-Oct-20", format = "%d-%B-%y")
#[1] "2020-10-31"
This also works when you have anything after 2-digit year. See
as.Date("31-Oct-20ABC", format = "%d-%B-%y")
#[1] "2020-10-31"
R tries to "auto-complete" when there is less information, it returns some (incorrect) date for
as.Date("31-Oct-20", format = "%d-%B-%Y")
#[1] "0020-10-31"
but returns NA for
as.Date("31-Oct-ABC20", format = "%d-%B-%y")
#[1] NA

week-dates in R [duplicate]

This question already has answers here:
Transform year/week to date object
(2 answers)
Closed 4 years ago.
In R, how do I convert the string 1/2010 (week 1 of 2010) into a Date or POSIXct (or POSIXlt) object?
I tried
as.Date('1/2010', "%W/%Y")
[1] "2010-06-29"
I also tried
strptime('1/2010', "%W/%Y")
[1] "2010-06-29 BRT"
But these are clearly not what I want.
In the end, I guess doesn't really matter which exact is picked, so long as I can correctly re-convert this to "weeks since origin".
library(splitstackshape)
date <- c("1/2013","3/2013")
date = data.frame(date)
df = data.frame(cSplit(date,"date","/"))
colnames(df) = c("week", "year")
df$date = as.Date(paste(df$year, df$week, 1, sep="-"), "%Y-%U-%u")

Resources