Is there a "dd-mmm-yyyy" format (e.g., "13-feb-1980") in the R?

Asked

Viewed 57 times

1

I wonder if there is the format "yyyy-MES ABBREVIATED-dd"

Example:

library(tidyverse)
library(lubridate)

df<-data.frame(dt=c("1980-02-13", "1983-08-03"))
df

          dt
1 1980-02-13
2 1983-08-03

I can put the abbreviated month by separating the columns:

df2<-df %>% 
  mutate(dt=ymd(dt),
         ano=year(dt),
         mes=month(dt, abbr = T, label = T))

          dt  ano mes
1 1980-02-13 1980 fev
2 1983-08-03 1983 ago

However, I would like to know if there is any way to generate an output of the following type:

dt
1 1980-fev-13
2 1983-ago-03

Is it possible or am I mistaken?

2 answers

4

With tidyverse:

library(tidyverse)
library(lubridate)

df %>% 
  mutate(across(.cols = dt, .fns = ~ str_to_lower(month(., abbr = T, 
  label = T)), .names = "mes")) %>% 
  separate(col = dt, into = c('year', 'month', 'day'), sep = "-") %>% 
  unite(col = "dt", year, mes, day, sep = "-") %>% 
  select(-month)

           dt
1 1980-fev-13
2 1983-ago-03

To get the date in format dd//mm//yyyy just adjust the next line:

separate(col = dt, into = c('day', 'month', 'year'), sep = "-")

           dt
1 13-fev-1980
2 03-ago-1983

3


Data from reply user name @neves.
In a baseline R, see the formats here.

format(as.Date(df$dt), "%d-%b-%Y")
#[1] "13-fev-1980" "03-ago-1983"

If the column is already class "Date", see at the end of the answer, just use the format.

format(df$dt, "%d-%b-%Y")
#[1] "13-fev-1980" "03-ago-1983"

Dice

df <-
structure(list(dt = structure(c(3695, 4962), class = "Date")), 
row.names = c(NA, -2L), class = "data.frame")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.