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"
Related
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 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"
I have a data frame with a some Dates, I have to convert a format "1996-04-22" to "1996-04" in R.
I have the good conversion but not in data type Date and when I tried to convert this to data type Date, I get "NA" everywhere.
//meteo is my date frame and date is where the dates are stocked
What I do :
meteo$date = format(meteo$date, "%Y-%m")
meteo$date = as.Date(meteo$date, format = "%Y-%m")
use zoo library's function yearmon to get year and month. Date needs day also.
library(zoo)
format(as.yearmon(substring(toString(MY_DATE_VARIABLE), 1, 7 )), "%Y-%m")
This question already has answers here:
How to convert variable with mixed date formats to one format?
(3 answers)
Closed 5 years ago.
I have two dataframe with two different format of date the first is "%d/%m/%Y %H:%M:%S" and the second "%Y-%m-%d %H:%M:%S".
I want to create a function that convert to POSIXct by indicating the format.
My code:
date_func <- function(df){
colnum <- grep("DATE", colnames(df))
df[, (colnum) := lapply(.SD, dmy_hms), .SDcols = colnum]
return(df)
}
For the first format it's works but for the second I have only NA values after the conversion.
So, how can I create a function that convert to POSIXct whatever the indicated format?
Thanks for your help.
Package lubridate provides very good option to handle date/time in heterogeneous format. The parse_date_time can be used. A simple example on converting date/time in format specified in OP are:
library(lubridate)
>parse_date_time(c("01/12/2016 01:11:54", "2015-12-31 10:05:11"), c("dmY HMS", "Ymd HMS"))
# [1] "2016-12-01 01:11:54 UTC" "2015-12-31 10:05:11 UTC"