Subset month, day, hour, minute, second from a date [duplicate] - r

This question already has answers here:
strptime, as.POSIXct and as.Date return unexpected NA
(2 answers)
Changing date format in R
(7 answers)
Closed 1 year ago.
I have a column in a dataframe which contains dates eg:
Date <- as.Date("2012-12-01 00:00:00")
Note: the actual dataframe format is "unknown"
I want to subset the month, date, hour from this dataframe and used this code
dmH <- as.POSIXct(Date, format="%Y%m%d %H%M%s")
dmH <- format(dmH, format="%m%d %H%M%s")
which returns a character format as below
"1201 02001354320000"
During this process it changed from UTC to EET so it starts at 02:00:00 and I don't know how to omit this change.
Most importantly, I need to have it in date format to be able to use it in a ggplot but I wasn't able to find any way to convert it, no matter what and how I tried.
EDIT:
As #Cath mentioned in the comment I tried to use that code but as.Date function returns only the year, month, day without the time. As a result, when I then try format function for any other time of the day it returns "00".
As opposed to as.Date I used again as.POSIXct and now it returns the right format (since I used the hyphen and %S in the "format" argument as you recommended). But still this is in character format which I need in date format.
So I used again mdH <- as.POSIXct(mdH, format = "%m-%d %H:%M:%S") on the formatted dataframe(mdH) as well as strptime to change it to date format but both return also the current year.
Note that if I use directly dmH <- strptime(as.character(Date), format="%m-%d %H:%M:%S") (as in one of the threads you recommended) it returns NA. Am I missing something? I can't resolve my issue

Related

Lubridate Date parsing is one year off [duplicate]

This question already has answers here:
date functions in R return wrong year
(2 answers)
Closed 3 years ago.
Within R, I'm trying to convert a text string into a Date variable type using lubridate's as.Date function.
I have a vector of values such as:
Dates
11/28/2019
11/29/2019
I am attempting to convert these to standard date variables using this as.Date function:
as.Date(Dates, "%m/%d/%y")
I do not receive an error message, and it correctly interprets the month and date, but for some reason it's outputting the wrong year - one year ahead:
"2020-11-28"
"2020-11-29"
I have no earthly idea why it is incorrectly interpreting the year in this way. Any help is appreciated!
We need to use %Y for 4 digit year as %y refers to only 2 digit
as.Date(Dates, "%m/%d/%Y")
Or using lubridate, this would be resolved
library(lubridate)
mdy(Dates)
Or with anydate from anytime
library(anytime)
anydate(Dates)

Using as.Date function in R on columns with blank cells and dates [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 3 years ago.
I have encountered a difficulty, trying to use the as.Date function (in R) on a data frame to preserve date format. The date column consists of blank cells (i.e. missing dates) and observed dates in the format month/year (e.g. 8/2019).
As mentioned earlier, I have tried using the as.Date function but the column for the dates turns blank completely (i.e. no dates are reported). Below is the code I am using:
df$date <- df$date<- as.Date(df$date, format='%m/%Y') #df is the data frame
The expected results should have the observed dates and the missing dates replaced with NA. I greatly appreciate your help.
You need to add a date component to make it a complete date. Once you do that it is easy to convert it into an actual date object
as.Date(paste0("1/", "8/2019"), "%d/%m/%Y")
#[1] "2019-08-01"
Or using dmy from lubridate
lubridate::dmy(paste0("1/", "8/2019"))

How do I stop implicit date conversion when using ifelse with date time data? [duplicate]

This question already has answers here:
How to prevent ifelse() from turning Date objects into numeric objects
(7 answers)
Closed 4 years ago.
I have a data frame that contains one column that is a series of dates, collected via a Google form. The date and time were collected separately. The data was entered by selecting a day from a calendar, and the date was entered manually - should have been a 24-hour clock, but the field appears to have just checked that the hour and minute were in the correct range.
I've read the file in from .csv . I converted the date time character field (as read in from the .csv) to a date time format in a new variable by using as.POSIXct(foo$When, tz="NZ", format="%Y-%m-%d %H:%M"). The dates and times were correctly constructed.
Except: I have some incorrect date/time entries in the original data. These have all been set to NA in the new field, as you expect. For those that do include a time, I have been trying to fix them while still retaining a POSIXct format.
I have been unsuccessful.
Here is an example of the data I have, and what I have tried to do:
TestDataForHelp <- data.frame(OldDateTime =
c("2013-12-04 21:10", "2013-12-15 09:07", "2014-01-01 06:27",
"2014-11-02 21:15", "2014-11-07 23:00", "2015-01-04 21:42",
"201508-11-02 20:15", "201508-11-02 20:15", "2017-11-02"))
TestDataForHelp$ActualDateTime <-
as.POSIXct(TestDataForHelp$OldDateTime, tz="NZ", format="%Y-%m-%d %H:%M")
TestDataForHelp$FixedDateTime <-
ifelse(TestDataForHelp$OldDateTime=="201508-11-02 20:15",
as.POSIXct("2015-11-02 20:15", tz="NZ", format="%Y-%m-%d %H:%M"),
TestDataForHelp$ActualDateTime)
The new variable, FixedDateTime, does not have a POSIXct type. It has been implicitly converted to a numeric type. How can I retain the POSIXct format from ActualDateTime and not have the implicit type conversion?
I would like to not have FixedDateTime but, rather, put the corrected data into ActualDateTime. The ifelse() seems to be the part of the code causing the format to shift from POSIXct to numeric. If I do:
TestDataForHelp$CopiedDateTime <- TestDataForHelp$ActualDateTime
The new variable, that is simply a copy of the original, retains the POSIXct type.
The previous question linked in the comments relates to date values only, not date time values. The data manipulation becomes more complicated with dealing with date time values, given that mine also do not include seconds. The other difference is that the original variable contains a mix of date, date-time, and incorrect date-time values, whereas that previous question had values that were all the same. It was unclear whether the non-uniform content of the variable was causing the problem.
Edit: I fixed the problem by fixing the strings before I converted them to dates. This removed the need to try to loop through the dates.
I can replicate the numeric answer, but not explain it. It is however calculating the results correctly for you. I'm not sure why it's returning as a numeric. However, the conversion from numeric to date is easy enough if you know the origin, which should be 1970-01-01. So I believe the following does the trick:
(Note, the first block is just what you already have)
TestDataForHelp$FixedDateTime <- ifelse(TestDataForHelp$OldDateTime=="201508-11-02 20:15",
as.POSIXct("2015-11-02 20:15", tz="NZ", format="%Y-%m-%d %H:%M"),
TestDataForHelp$ActualDateTime)
TestDataForHelp$FixedDateTime <- as.POSIXct(TestDataForHelp$FixedDateTime,
origin = as.POSIXct("1970-01-01", tz="NZ"))

%b-%Y date conversion gives NA [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 4 years ago.
I am trying to convert character strings to Dates in R. These are examples of the character strings:
"Aug-1973" "Aug-1974" "Aug-1975" "Aug-1976" "Aug-1977"
I run the following line on date strings similar to the ones above:
exportsDF$Date <- as.Date(as.character(exportsDF$Date), format = "%b-%Y")
This returns NAs for all values. The step where I convert the dates column to characters returns the correct values. Any ideas why the as.Date() command is not working? There are no NAs or missing values in the data. Every value has a "%b-%Y" format.
Any help is appreciated!
The date format needs a day as well, so you could add an arbitrary day of the month. Here, I've chosen the first day:
dates <- c("Aug-1973", "Aug-1974", "Aug-1975", "Aug-1976", "Aug-1977")
res <- as.Date(paste0("01-", dates), format = "%d-%b-%Y")
print(res)
#[1] "1973-08-01" "1974-08-01" "1975-08-01" "1976-08-01" "1977-08-01"
The reason is that the underlying Date data type is an integer counting the days since some reference day. Specifically, the number of days since 1970-01-01. See ?Date.
The Date object res can now be displayed as you please via
format(res, "%B-%Y")
#[1] "August-1973" "August-1974" "August-1975" "August-1976" "August-1977"
or similar.
The month(res) function and its cousins are also helpful. See ?month.

how to covert dates with just year-month into year-month-date in R? [duplicate]

This question already has an answer here:
Generating a date from a string with a 'Month-Year' format
(1 answer)
Closed 4 years ago.
I have a list of program dates as character strings in the following format
program.date.have <-c('Sep-14','Aug-14','Sep-16')
I am assuming that all these programs started on the first day of each month, and I want the program.date to end up like
program.date.want<-c('2014-09-01', '2014-08-01, '2016-09-01') or in YYYY-MM-DD format.
To start somewhere I have decided to covert the character strings into the date format in the following way
program.date.have<-c('Sep-14','Aug-14','Sep-16')
betterDates <- as.Date(program.date,
format = "%m-%y")
But even that does not seem to work. how do I use values in program.date variable to be converted into format I want in program.date.want
We can use as.yearmon from zoo, specify the format, and wrap with as.Date which automatically generates the 'day' as the first of the month.
library(zoo)
as.Date(as.yearmon(program.date.have, "%b-%y"))
#[1] "2014-09-01" "2014-08-01" "2016-09-01"
Or a base R option is to paste the '01' at the start or end and then specify the appropriate format in as.Date
as.Date(paste0(program.date.have, "-01"), "%b-%y-%d")
#[1] "2014-09-01" "2014-08-01" "2016-09-01"

Resources