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"
Related
I have a dataframe with date in character format. I want to convert date into date format. When i try this:
data$date<-as_date(data$date, tz = NULL, format = NULL)
It converts dataframe into list and also date still remains in character format. what i am doing wrong here?
Thanks
I have a large amount of data with 18000 rows. One of the columns is a combined date and time. In the csv file the format is "05/03/2020 05:00:00 PM". When i read the csv into R it turns into a character with the format "05-Mar-2020 17:00:00". I need R to understand this column as the date and time.
I have tried the following and it just replaces the DateTimes with NAs.
'dat' is the dataframe and 'DateTime' is the column name.
dat[['DateTime']] <- as.POSIXct(dat[['DateTime']], format = "%d%-%m-%Y %H:%M:%S")
x <- "05-Mar-2020 17:00:00"
as.POSIXct(x, format = "%d-%b-%Y %H:%M:%S")
# [1] "2020-03-05 17:00:00 CET"
use help(strptime) to see all format codes, you see that %b is used for the abbreviated month name instead of %m and that you had a redundant % sign as well.
Or you can use the lubridate package and do:
lubridate::dmy_hms(x)
I have a column in a dataset right now formatted for example as "Aug-19" which represents 08/01/2019 in mm/dd/yyyy format. How do I convert this into a format that can be read by R?
You can use lubridate package.
dates <- c("Aug-19", "Sep-19")
dates_myd <- paste0(dates, "-01")
lubridate::myd(dates_myd)
With lubridate we can also do
myd(dates, truncated = 1)
#[1] "2019-08-01" "2019-09-01"
data
dates <- c("Aug-19", "Sep-19")
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"
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");