how to split date in one column - r

I want to split date from DATE_H_REAL and have only date in my column
DATE_H_REAL DATE_H_REAL
02/02/2016 16:17 02/02/2016
I tried this but didn't work:
dataset1$DATE_H_REAL <- strptime(dataset1$DATE_H_REAL, format="%d/%m/%Y")
dataset1$DATE_H_REAL <- as.POSIXct(dataset1$DATE_H_REAL)

If you want to convert to a Date class, you can use as.Date() and precise the format of your input:
dataset1$DATE_H_REAL=as.Date(dataset1$DATE_H_REAL,format="%d/%m/%Y")
This returns a date:
[1] "2016-02-02"
In case your input column doesn't have a constant format, you can first extract the date before converting it to a Date class:
dataset1$DATE_H_REAL=gsub(".*(\\d{2}/\\d{2}/\\d{4}).*","\\1",dataset1$DATE_H_REAL)
dataset1$DATE_H_REAL=as.Date(dataset1$DATE_H_REAL,format="%d/%m/%Y");

Related

How to convert datetime to mmm-yy format in R

I have a dataframe where there is a column with datetime.
2020-12-02 13:14:19
How can I covert it in mmm-yy format. To get output like Dec-20.
Convert to POSIXct (if it is not already) and then use format
x <- '2020-12-02 13:14:19'
format(as.POSIXct(x), '%b-%y')
#[1] "Dec-20"

R convert string "Jan\n1990" to date format

I have a csv file with the first column (named "month") having month data in the format
"Jan\n1990", "Feb\n1990" and so on.
When I read the file in R using read.csv function (with stringsAsFactors = FALSE) it reads the "month" column as 'chr'. I want to convert it into date format. I tried
month_1 <- as.Date(f4$month)
But it gives the error
Error in charToDate(x) :
character string is not in a standard unambiguous format
How do I convert the first column into a date formatted column?
In r, using the function parse_date_time from lubridate package, you can convert your character string into a date format:
date <- c("Jan\n1990","Feb\n1990") # Example of character strings to convert into dates
library(lubridate)
parse_date_time(date, order = "bY")
[1] "1990-01-01 UTC" "1990-02-01 UTC"
A solution based on as.Date:
date <- c("Jan\n1990","Feb\n1990")
as.Date(paste0("1",date), format="%d%b\n%Y")
# [1] "1990-01-01" "1990-02-01"
Another option is as.yearmon from zoo
library(zoo)
as.Date(as.yearmon(date))
#[1] "1990-01-01" "1990-02-01"

Conversion of numeric vector to date

I have a dataframe with a date column like the following:
19870703
19920929
20051015
20030613
How can I change the numeric Data to a Date format like %Y%m%d?
Neither as.date() nor as.POSIX.ct() works?
Using lubridate
date <- ymd("19870703")
in place of date you can put your column name
as.Date("19870703", "%Y%m%d")
[1] "1987-07-03"
Apply for entire column.
library(date)
as.date(your.date.vector)

How do I keep date order after using the split function in R?

I have a csv which is has a list of lat-long coordinates with dates for each. I have code to split the data by date
X <- split(mydata, mydata$date)
But when I run the function the data is sorted numerically rather than by date. So for example this:
long lat date
26.71360 -31.67479 04-04-2013
26.53347 -31.54144 05-04-2013
26.36730 -31.39950 06-04-2013
26.15438 -31.27452 04-05-2013
26.25447 -31.06523 04-05-2013
26.31591 -30.92225 04-05-2013
Would be converted to this: Note that dates are d/m/y
long lat date
26.71360 -31.67479 04-04-2013
26.53347 -31.54144 04-05-2013
26.36730 -31.39950 04-05-2013
26.15438 -31.27452 04-05-2013
26.25447 -31.06523 05-04-2013
26.31591 -30.92225 06-04-2013
How can I keep the order?
Thanks
You need to make sure that your 'date' variable is of class Date. Conversion from character to Date representations of dates can be made with as.Date. Because the format of your input dates is neither "%Y-%m-%d" nor "%Y/%m/%d" (see format argument in ?as.Date) you need to specify format explicitly. See ?strptime for abbreviations of the various time components.
df$date <- as.Date(df$date, format = "%d-%m-%Y")

Change factor to a date format in R

I have a file with dates in the format YYYY-mm. I am trying to prepare my data for a time series analysis and therefore need to convert the formats from factor to a Date format.
What I've tried:
x <- '2011-11'
as.Date(as.character(a), "%Y-%m")
the last line gives me an output NA.
Thank you!
"2011-11" is not a date (where's the day)? Either add a day:
a <- '2011-11'
as.Date(paste0(a,'-01'))
or use zoo's yearmon class:
library(zoo)
as.yearmon(a)
'2011-11' isn't a date. Use paste(your.year.month.strs, '01', sep='-') to add the day component to your strings, then call as.Date with "%Y-%m-%d".

Resources