How to transform the class of a column "factor" in "date" inside a data.frame?

Question:

I have a base extracted in csv (dados_base) , with the period from Dec/2017 to Jan/2019, which has three columns: USUARIO , DT_PAGTO and VL_PED_PG , where the first field represents the users with the "integer" class, the second field represents the payment date with the "factor" class and the third field represents the amount in reais with the "numeric" class. In my case I need to transform the DT_PAGTO column of class "factor" to "date" .

I tried using mutate :

dados_base2 = dados_base %>%
  mutate(DT_PAGTO = as.Date(DT_PAGTO,"%d/%m/%y"))

class(dados_base$DT_PAGTO)
[1] "Date"

print(dados_base2$DT_PAGTO)
[1] "2020-12-20"

In my attempt the class is successfully transformed, but the date format does not match the original csv date. Is there another way to transform the class? What mistake am I making?

Here's the dput to help understand:

dput(head(dados_base, 50))
structure(list(USUARIO = c(282746L, 6651152L, 6622750L, 183147L, 
833097L, 5654010L, 62129L, 640459L, 283914L, 6833181L, 843495L, 
6696269L, 179480L, 71173L, 203562L, 979164L, 6747726L, 5683083L, 
6797883L, 245945L, 373197L, 25797L, 599336L, 827926L, 6728407L, 
6815081L, 244841L, 6797883L, 370121L, 211326L, 6825906L, 35976L, 
6614515L, 83303L, 373197L, 473348L, 350239L, 191273L, 364387L, 
294993L, 6781972L, 288423L, 6447749L, 173166L, 165653L, 6652688L, 
6690957L, 978119L, 597592L, 487315L), DT_PAGTO = structure(c(260L, 
260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 
260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 
260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 
260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 260L, 
260L, 260L, 260L, 260L, 260L), .Label = c("01/01/2018", "01/01/2019", 
"01/02/2018", "01/03/2018", "01/04/2018", "01/05/2018", "01/06/2018", 
"01/07/2018", "01/08/2018", "01/09/2018", "01/10/2018", "01/11/2018", 
"01/12/2018", "02/01/2018", "02/01/2019", "02/02/2018", "02/03/2018", 
"02/04/2018", "02/05/2018", "02/06/2018", "02/07/2018", "02/08/2018", 
"02/09/2018", "02/10/2018", "02/11/2018", "02/12/2018", "03/01/2018", 
"03/01/2019", "03/02/2018", "03/03/2018", "03/04/2018", "03/05/2018", 
"03/06/2018", "03/07/2018", "03/08/2018", "03/09/2018", "03/10/2018", 
"03/11/2018", "03/12/2018", "04/01/2018", "04/01/2019", "04/02/2018", 
"04/03/2018", "04/04/2018", "04/05/2018", "04/06/2018", "04/07/2018", 
"04/08/2018", "04/09/2018", "04/10/2018", "04/11/2018", "04/12/2018", 
"05/01/2018", "05/01/2019", "05/02/2018", "05/03/2018", "05/04/2018", 
"05/05/2018", "05/06/2018", "05/07/2018", "05/08/2018", "05/09/2018", 
"05/10/2018", "05/11/2018", "05/12/2018", "06/01/2018", "06/01/2019", 
"06/02/2018", "06/03/2018", "06/04/2018", "06/05/2018", "06/06/2018", 
"06/07/2018", "06/08/2018", "06/09/2018", "06/10/2018", "06/11/2018", 
"06/12/2018", "07/01/2018", "07/01/2019", "07/02/2018", "07/03/2018", 
"07/04/2018", "07/05/2018", "07/06/2018", "07/07/2018", "07/08/2018", 
"07/09/2018", "07/10/2018", "07/11/2018", "07/12/2018", "08/01/2018", 
"08/01/2019", "08/02/2018", "08/03/2018", "08/04/2018", "08/05/2018", 
"08/06/2018", "08/07/2018", "08/08/2018", "08/09/2018", "08/10/2018", 
"08/11/2018", "08/12/2018", "09/01/2018", "09/01/2019", "09/02/2018", 
"09/03/2018", "09/04/2018", "09/05/2018", "09/06/2018", "09/07/2018", 
"09/08/2018", "09/09/2018", "09/10/2018", "09/11/2018", "09/12/2018", 
"10/01/2018", "10/01/2019", "10/02/2018", "10/03/2018", "10/04/2018", 
"10/05/2018", "10/06/2018", "10/07/2018", "10/08/2018", "10/09/2018", 
"10/10/2018", "10/11/2018", "10/12/2018", "11/01/2018", "11/01/2019", 
"11/02/2018", "11/03/2018", "11/04/2018", "11/05/2018", "11/06/2018", 
"11/07/2018", "11/08/2018", "11/09/2018", "11/10/2018", "11/11/2018", 
"11/12/2018", "12/01/2018", "12/01/2019", "12/02/2018", "12/03/2018", 
"12/04/2018", "12/05/2018", "12/06/2018", "12/07/2018", "12/08/2018", 
"12/09/2018", "12/10/2018", "12/11/2018", "12/12/2018", "13/01/2018", 
"13/01/2019", "13/02/2018", "13/03/2018", "13/04/2018", "13/05/2018", 
"13/06/2018", "13/07/2018", "13/08/2018", "13/09/2018", "13/10/2018", 
"13/11/2018", "13/12/2018", "14/01/2018", "14/01/2019", "14/02/2018", 
"14/03/2018", "14/04/2018", "14/05/2018", "14/06/2018", "14/07/2018", 
"14/08/2018", "14/09/2018", "14/10/2018", "14/11/2018", "14/12/2018", 
"15/01/2018", "15/01/2019", "15/02/2018", "15/03/2018", "15/04/2018", 
"15/05/2018", "15/06/2018", "15/07/2018", "15/08/2018", "15/09/2018", 
"15/10/2018", "15/11/2018", "15/12/2018", "16/01/2018", "16/01/2019", 
"16/02/2018", "16/03/2018", "16/04/2018", "16/05/2018", "16/06/2018", 
"16/07/2018", "16/08/2018", "16/09/2018", "16/10/2018", "16/11/2018", 
"16/12/2018", "17/01/2018", "17/01/2019", "17/02/2018", "17/03/2018", 
"17/04/2018", "17/05/2018", "17/06/2018", "17/07/2018", "17/08/2018", 
"17/09/2018", "17/10/2018", "17/11/2018", "17/12/2018", "18/01/2018", 
"18/01/2019", "18/02/2018", "18/03/2018", "18/04/2018", "18/05/2018", 
"18/06/2018", "18/07/2018", "18/08/2018", "18/09/2018", "18/10/2018", 
"18/11/2018", "18/12/2018", "19/01/2018", "19/01/2019", "19/02/2018", 
"19/03/2018", "19/04/2018", "19/05/2018", "19/06/2018", "19/07/2018", 
"19/08/2018", "19/09/2018", "19/10/2018", "19/11/2018", "19/12/2018", 
"20/01/2018", "20/01/2019", "20/02/2018", "20/03/2018", "20/04/2018", 
"20/05/2018", "20/06/2018", "20/07/2018", "20/08/2018", "20/09/2018", 
"20/10/2018", "20/11/2018", "20/12/2017", "20/12/2018", "21/01/2018", 
"21/01/2019", "21/02/2018", "21/03/2018", "21/04/2018", "21/05/2018", 
"21/06/2018", "21/07/2018", "21/08/2018", "21/09/2018", "21/10/2018", 
"21/11/2018", "21/12/2017", "21/12/2018", "22/01/2018", "22/01/2019", 
"22/02/2018", "22/03/2018", "22/04/2018", "22/05/2018", "22/06/2018", 
"22/07/2018", "22/08/2018", "22/09/2018", "22/10/2018", "22/11/2018", 
"22/12/2017", "22/12/2018", "23/01/2018", "23/01/2019", "23/02/2018", 
"23/03/2018", "23/04/2018", "23/05/2018", "23/06/2018", "23/07/2018", 
"23/08/2018", "23/09/2018", "23/10/2018", "23/11/2018", "23/12/2017", 
"23/12/2018", "24/01/2018", "24/01/2019", "24/02/2018", "24/03/2018", 
"24/04/2018", "24/05/2018", "24/06/2018", "24/07/2018", "24/08/2018", 
"24/09/2018", "24/10/2018", "24/11/2018", "24/12/2017", "24/12/2018", 
"25/01/2018", "25/02/2018", "25/03/2018", "25/04/2018", "25/05/2018", 
"25/06/2018", "25/07/2018", "25/08/2018", "25/09/2018", "25/10/2018", 
"25/11/2018", "25/12/2017", "25/12/2018", "26/01/2018", "26/02/2018", 
"26/03/2018", "26/04/2018", "26/05/2018", "26/06/2018", "26/07/2018", 
"26/08/2018", "26/09/2018", "26/10/2018", "26/11/2018", "26/12/2017", 
"26/12/2018", "27/01/2018", "27/02/2018", "27/03/2018", "27/04/2018", 
"27/05/2018", "27/06/2018", "27/07/2018", "27/08/2018", "27/09/2018", 
"27/10/2018", "27/11/2018", "27/12/2017", "27/12/2018", "28/01/2018", 
"28/02/2018", "28/03/2018", "28/04/2018", "28/05/2018", "28/06/2018", 
"28/07/2018", "28/08/2018", "28/09/2018", "28/10/2018", "28/11/2018", 
"28/12/2017", "28/12/2018", "29/01/2018", "29/03/2018", "29/04/2018", 
"29/05/2018", "29/06/2018", "29/07/2018", "29/08/2018", "29/09/2018", 
"29/10/2018", "29/11/2018", "29/12/2017", "29/12/2018", "30/01/2018", 
"30/03/2018", "30/04/2018", "30/05/2018", "30/06/2018", "30/07/2018", 
"30/08/2018", "30/09/2018", "30/10/2018", "30/11/2018", "30/12/2017", 
"30/12/2018", "31/01/2018", "31/03/2018", "31/05/2018", "31/07/2018", 
"31/08/2018", "31/10/2018", "31/12/2017", "31/12/2018"), class = "factor"), 
    VL_PED_PG = c(30, 30, 30, 10, 10, 20, 30, 50, 10, 46.2, 20, 
    50, 22.8, 30, 50, 20, 30, 50, 10, 30, 10, 20, 30, 40, 15.2, 
    20, 20, 10, 20, 10, 10, 10, 40.03, 20, 8, 50, 10, 30, 30, 
    30, 10, 20, 50, 30, 20, 20, 10, 20, 30, 20)), row.names = c(NA, 
50L), class = "data.frame") 

Answer:

I think the as.Date function is pretty bad. I often have problems with it and I don't know exactly how to solve it. So I suggest using the lubridate package to work with dates. Instead of putting formats like "%d/%m/%y" to create date objects, the lubridate package has specific and very intuitive functions to do this.

As your dates are in day, month and year format, the proper function of lubridate to convert these values ​​to date is dmy (day, month, year):

library(lubridate)
dados_base$DT_PAGTO <- dmy(dados_base$DT_PAGTO)
is.Date(dados_base$DT_PAGTO)
[1] TRUE
Scroll to Top