How to convert a column as date with special character? - r

In my dataframe I have a date column and I would like to convert it from character to date in the format d/m/y.
The head of my data:
head(df$date)
[1] [17/Jun/2019:08:33:49 [17/Jun/2019:08:38:20 [17/Jun/2019:08:38:24 [17/Jun/2019:09:52:42
[5] [17/Jun/2019:09:52:44 [17/Jun/2019:09:52:45
I used this but it converts every value into NA
df$date = as.Date(df$date, "[%d%b%y")

Try:
df$date <- strptime(df$date, format = "[%d/%b/%Y:%H:%M:%S")
df$date <- as.Date(df$date, format = "%d/%m/%y")

Using a tidyverse approach, looks like the dmy_hms() function accommodates that atypical first colon:
library(lubridate)
df <- df %>% mutate(date = dmy_hms(date), date = date(date))
Using your first value as an example:
date <- "17/Jun/2019:08:33:49"
date <- dmy_hms(date)
date
#[1] "2019-06-17 08:33:49 UTC"
date <- date(date) #or all in one line, date <- dmy_hms(date) %>% date()
date
#[1] "2019-06-17"

Assuming this is your input
x <- c("[17/Jun/2019:08:33:49", "[17/Jun/2019:08:38:20",
"17/Jun/2019:08:38:24", "[17/Jun/2019:09:52:42")
First convert it into POSIXct format and then to Date
as.Date(as.POSIXct(x, format = "[%d/%b/%Y:%T"))
#[1] "2019-06-17" "2019-06-17" "2019-06-17" "2019-06-17"
or any other format
format(as.POSIXct(x, format = "[%d/%b/%Y:%T"), "%d/%m/%Y")
#[1] "17/06/2019" "17/06/2019" "17/06/2019" "17/06/2019"

If you want to convert into Date object try this.
df$date = as.Date(df$date,format="[%d/%b/%Y:%H:%M:%S")
If you want to retain time as well, then try the following.
df$date = as.POSIXct(df$date,format="[%d/%b/%Y:%H:%M:%S")
Best wishes.

Related

Factor variable into weekdays

I have a variable for the date of medical admission. However, it is not properly formatted. It is a factor and formatted as "DDMMYEAR HRMN", like "01012016 1215", which should mean "01-01-2016 12:15". How can I reformat it and assign weekdays?
You can use lubridate to parse the date, then weekdays from base R to get the day of week as a character.
library(lubridate)
d <- dmy_hm("01012016 1215")
weekdays(d)
Use as.POSIXct/strptime to convert to date time and then use weekdays.
df$date <- as.POSIXct(df$date, format = '%d%m%Y %H%M', tz = 'UTC')
df$weekday <- weekdays(df$date)
For example,
string <- '01012016 1215'
date <- as.POSIXct(string, format = '%d%m%Y %H%M', tz = 'UTC')
date
#[1] "2016-01-01 12:15:00 UTC"
weekdays(date)
#[1] "Friday"

Custom Date Transformation in R

Using R
How do we convert "yyyymm" to "yyyy-mm-01" across all the rows?
Eg: "201603" to "2016-03-01" (ie "yyyy-mm-dd" format)
PS: Here, (dd = 01) is the default date for all 12 months. ie("2016-01-01" , "2016-02-01" , etc...)
A simple paste solution:
x <- "201603"
paste0(substr(x, 1,4), "-", substr(x, 5,6), "-01")
[1] "2016-03-01"
If you want to transform as date:
as.Date(paste0(201603, 01), format = "%Y%m%d")
This will create the 2016-03-01 format as the date and not as a character.
If you want to use in all rows on column Date
data <- data %>%
mutate(Date = as.Date(paste(Date, 01) format = "%Y%m%d")
additional solution
library(lubridate)
library(stringr)
x <- c("201603")
ymd(str_c(x,"01"))
[1] "2016-03-01"

Removing time stamp to include just the month and day

I have a table called "rejected_at" in the dataframe "Job_applications". The format is: "M/DD/YYYY H:M:S PM/AM". Now i want to create a new table that just has "M/DD". How would i do this?enter image description here
We can convert to DateTime class and then extract the month and day
v1 <- as.POSIXct(df1$rejected_at, "%m/%d/%Y %H:%M:%S")
format(v1, "%d")
format(v1, "%m")
Or if we the format needed is %m/%d
format(v1, "%m/%d")
Or using tidyverse
library(dplyr)
library(lubridate)
df1 %>%
mutate(rejected_at = mdy_hms(rejected_at),
day = day(rejected_at),
month = month(rejected_at))
Assuming the rejected_at column is just text, you could try using sub here:
x <- "12/30/2018 4:05:44 PM"
sub("/[^/]+$", "", x)
[1] "12/30"
Or, in your case:
Job_applications$new_col <- sub("/[^/]+$", "", Job_applications$rejected_at)
Edit:
If you also wanted to retain the year, then we can try using sub with a capture group targeting the full date:
sub("(\\d+/\\d+/\\d+).*", "\\1", x)
[1] "12/30/2018"
You can do it using lubridate
library(lubridate)
new_date <- mdy_hms("08/14/2017 09:59:06 PM")
new1 <- paste0(month(new_date),"/",day(new_date))

factor to date returns NA

I am sorry but I struggle with this:
mydate <- factor("2016-10-25")
as.Date(mydate, format = "%Y-%M-%D")
it returns NA. Any ideas? Thanks!
You need to use small letters for month and day ("%Y-%m-%d") instead of capital letters ("%Y-%M-%D").
mydate <- factor("2016-10-25")
as.Date(mydate, format = "%Y-%m-%d")
"2016-10-25"
You can use lubridate as follows:
mydate <- factor("2016-10-25")
require(lubridate)
ymd(mydate)

How to change date format from YYYY/MM/DD to DD/MM/YYYY

I have a column of dates which were read in as character values (yes, they are supposed to be the same):
str(df$date)
$ date : chr "30/08/2017" "30/08/2017" "30/08/2017" "30/08/2017"
I then convert the values to Date format:
str(df$date)
$ date : Date, format: "2017-08-30" "2017-08-30" "2017-08-30"
The problem is that no matter which method I choose to use, the resulting dates are always in YYYY/MM/DD format, which is not what I want; they should be in DD/MM/YYYY format.
I try:
df$date <- as.Date(df$date, format = "%d/%m/%Y")
df$date <- strptime(df$date, format = "%d/%m/%Y")
df$date <- as.POSIXct(df$date, format = "%d/%m/%Y")
and they all produce the same format.
I have read numerous similar Stack Overflow posts as well as some guides and have tried things like getting and setting my system locale (United Kingdom) and all is correct in that respect.
Where am I going wrong?
I try:
df$date <- as.Date(df$date, format = "%d/%m/%Y")
df$date <- strptime(df$date, format = "%d/%m/%Y")
df$date <- as.POSIXct(df$date, format = "%d/%m/%Y")
and they all produce the same format.
R has two very similarly named functions: strptime, which converts from character strings to Date data, and strftime, which converts Dates to formatted strings. To make matters worse, the documentation for these two functions is combined, so it can be very hard to keep their uses straight. You want strftime, in this case.
You can also use format:
date = c("30/08/2017", "30/08/2017", "30/08/2017", "30/08/2017")
date <- as.Date(date, format = "%d/%m/%Y")
# > date
# [1] "2017-08-30" "2017-08-30" "2017-08-30" "2017-08-30"
date = format(date, "%m/%d/%Y")
# > date
# [1] "08/30/2017" "08/30/2017" "08/30/2017" "08/30/2017"
Turns into character class:
# > class(date)
# [1] "character"
This will help you:
$variable = '2018/09/18';
$date = str_replace('/', '/', $variable);
echo date('d/m/Y', strtotime($date));
Please check and let me know if you need any more help.

Resources