Replace month abbreviation with number - r

For example: df$Date
SN Date
1 07-Mar-2019
2 06-Feb-2019
how do I set a condition to replace the value "Mar" = "03" and "Feb" = "02" in df$Date?
So that the output will be:
SN Date
1 07-03-2019
2 06-02-2019
anyone can help? Thank you

You can use as.Date. You can read about different formats at ?strptime
df$Date <- as.Date(df$Date, "%d-%b-%Y")
df
# SN Date
#1 1 2019-03-07
#2 2 2019-02-06
Or if you don't want to worry about format use dmy from lubridate
df$Date <- lubridate::dmy(df$Date)
Or anydate function from anytime.
df$Date <- anytime::anydate(df$Date)
To get output exactly in the same format as shown, we can do
df$Date <- format(as.Date(df$Date, "%d-%b-%Y"), "%d-%m-%Y")
df
# SN Date
#1 1 07-03-2019
#2 2 06-02-2019
data
df <- structure(list(SN = 1:2, Date = structure(2:1, .Label = c("06-Feb-2019",
"07-Mar-2019"), class = "factor")), class = "data.frame", row.names = c(NA, -2L))

Related

How can I convert number to hh:mm:ss?

I have a column in a dataframe that is the number of seconds past midnight. How would I got about converting that number to a time displayed as hh:mm:ss? For instance:
hrsecs
1563
13088
14309
becomes
Time
00:26:03
03:38:08
03:58:29
Convert the seconds to period (seconds_to_period) and use hms from hms package
library(lubridate)
library(dplyr)
df1 <- df1 %>%
transmute(Time = hms::hms(seconds_to_period(hrsecs)))
-output
df1
Time
1 00:26:03
2 03:38:08
3 03:58:29
data
df1 <- structure(list(hrsecs = c(1563L, 13088L, 14309L)),
class = "data.frame", row.names = c(NA,
-3L))
1) character output Convert to POSIXct and then format. No packages are used.
x <- c(1563, 13088, 14309)
tt <- format(as.POSIXct("1970-01-01") + x, "%T"); tt
## [1] "00:26:03" "03:38:08" "03:58:29"
or
tt <- format(structure(x, class = c("POSIXct", "POSIXt"), tzone = "UTC"), "%T")
tt
## [1] "00:26:03" "03:38:08" "03:58:29"
2) times class output If you want to be able to manipulate the times then this will express them internally as fractions of a day but render them as times.
library(chron)
times(tt)
## [1] 00:26:03 03:38:08 03:58:29

Convert date format from dd-mm-yyyy to dd/mm/yyyy

I would like convert my date format from dd-mm-yyyy to dd/mm/yyyy
Data:
date
1 22-Jul-2020
Current code:
format(as.Date(df$date, '%d:%m:%Y'), '%d/%m/%Y' )
[1] NA NA
Desired Output:
date
1 22/07/2020
The format in as.Date should match the input format. It is %d followed by -, then abbrevation for month (%b) followed by - and 4 digit year (%Y)
df$date <- format(as.Date(df$date, '%d-%b-%Y'), '%d/%m/%Y' )
df$date
#[1] "22/07/2020"
data
df <- structure(list(date = "22-Jul-2020"), class = "data.frame", row.names = "1")
You can try
library(lubridate)
df <- data.frame(date = c("22-Jul-2020"))
df$date <- dmy(df$date)
df$date <- format(df$date, format = "%d/%m/%Y")
# date
#1 22/07/2020

Add date of character class with another date

Can we add the date of Character class to another date (lag of specific date). I want to reduce by 05:30:00
df
Date
12:48:36
12:48:37
13:48:36
Required dateframe
df
Date
07:48:36
07:48:37
08:48:36
df <- structure(list(Date = structure(1:3, .Label = c("12:48:36", "12:48:37",
"13:48:36"), class = "factor")), class = "data.frame", row.names = c(NA,
-3L))
You could use as.ITime from data.table
library(data.table)
setDT(df)
df[, Date := as.ITime(Date) - as.ITime('05:00:00')]
df
# Date
# 1: 07:48:36
# 2: 07:48:37
# 3: 08:48:36
Edit: If you have stored Date as a factor (as in this example) you need to convert to character first
df[, Date := as.character(Date)]

Convert date to first day of the corresponding month from dataframe

I have a dataframe which is named as df
I need to convert each date to first day of that month.
Date Sales
02-12-2018 1000
03-11-2019 2000
24-08-2010 3000
Expected output
Date Sales Date2
02-12-2018 1000 01-12-2018
03-11-2019 2000 01-11-2019
24-08-2010 3000 01-08-2010
and so on. I cannot seem to be able to do that in R.
Convert to date and use floor_date from lubridate
library(lubridate)
floor_date(dmy(df$Date), 'month')
#[1] "2018-12-01" "2019-11-01" "2010-08-01"
data
df <- structure(list(Date = structure(1:3, .Label = c("02-12-2018",
"03-11-2019", "24-08-2010"), class = "factor"), Sales = c(1000L,
2000L, 3000L)), class = "data.frame", row.names = c(NA, -3L))
You can convert to date and then format the date with "01" in the days slot.
format(as.Date(df$Date, "%d-%m-%Y"), "%01-%m-%Y")
# [1] "01-12-2018" "01-11-2019" "01-08-2010"
We can also use a simple regex to do this (Using data provided by #RonakShah)
sub(".*?-", "01-", df$Date)
#[1] "01-12-2018" "01-11-2019" "01-08-2010"
An option with anydate
library(anydate)
format(anydate(df$Date), "%01-%m-%Y")

Convert monthly data from one type to another

I have a column with monthly date that has such type (number of month, _, year):
Date
9_2018
1_2013
12_2014
etc.
I want to convert this date format to a date of the following form (year, month):
New_Date
201809
201301
201412
How can I do this?
We can use zoo::as.yearmon to convert the date and then use format to get data in the required format.
format(zoo::as.yearmon(df$Date, "%m_%Y"), "%Y%m")
#[1] "201809" "201301" "201412"
Or can be done in base R as well by pasting an arbitrary date to year-month value we have.
format(as.Date(paste0("1_", df$Date), "%d_%m_%Y"), "%Y%m")
data
df <- structure(list(Date = structure(c(3L, 1L, 2L), .Label = c("1_2013",
"12_2014", "9_2018"), class = "factor")),
class = "data.frame",row.names = c(NA, -3L))
Padding zeros for month and then splitting at the "_":
library(stringr)
mon <- sapply(strsplit(Date, "_"), FUN="[", 1)
mon <- str_pad(mon, width=2, pad="0")
year <- sapply(strsplit(Date, "_"), FUN="[", 2)
paste0(year,mon)
[1] "201809" "201301" "201412"

Resources