How to format a "date" column inside of a data.frame in R?

Asked

Viewed 2,296 times

3

I’m having trouble formatting a column with dates in my database.

Example:

01/08/2018 06:02:44

I would like to format the column by removing the team and in another column indicate the corresponding month.

How can I do that?

3 answers

6

I’m a fan of using the package lubridate to solve any and all problems with date. See below how easily I got what interested you with very intuitive name functions:

library(lubridate)

data <- dmy_hms("01/08/2018 06:02:44")
data
[1] "2018-08-01 06:02:44 UTC"
date(data)
[1] "2018-08-01"
month(data)
[1] 8

For more package functions, turn ?lubridate in your terminal and descend to the link Index, at the bottom of the help page that will open.

  • My question is to format this date that is already within a data.frame. How can I format the date of an entire column?

  • Edits the post with a sample of the data. Uses the command dput for that reason.

5


First of all, let’s create a reproducible example.

library(tidyverse)
tabela <- data_frame(timestamp = Sys.time())

And then it is possible to solve the first question like this:

tabela %>% 
  mutate(
    coluna_sem_time = as.Date(timestamp)
  )
# A tibble: 1 x 2
  timestamp           coluna_sem_time
  <dttm>              <date>         
1 2019-01-30 17:46:01 2019-01-30 

And add the month

tabela %>% 
  mutate(
    mes = lubridate::month(timestamp)
  )
# A tibble: 1 x 2
  timestamp             mes
  <dttm>              <dbl>
1 2019-01-30 17:46:01     1

Or do both at once

tabela %>% 
  mutate(
    coluna_sem_time = as.Date(timestamp),
    mes = lubridate::month(timestamp)
  )
# A tibble: 1 x 3
  timestamp           coluna_sem_time   mes
  <dttm>              <date>          <dbl>
1 2019-01-30 17:46:01 2019-01-30          1

4

I also use the package a lot lubridate but in this case it is not necessary. Just use the function format() that is already loaded in the package base:

tabela <- data.frame(timestamp = Sys.time()) # criar um data.frame com a data

For objects in format POSIXct, use format() and specify what will be formatted on the date. For example, format(x, "%Y") preserve only the year in 4-digit format (e.g. 2019). Now format(x, "%y") will remove only the last two digits of the year (e.g. the "19" of "2019"). "%d" and "%m" extracts the day and month from the object respectively. Therefore:

> format(tabela$timestamp, "%d-%m-%Y")
[1] "31-01-2019"

> format(tabela$timestamp, "%m")
[1] "01"

Browser other questions tagged

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