2

I'm trying to convert a column in a table from integer to date with ymd().

The table is large one creating from merging several csv. The table structure is as follows:

Classes ‘data.table’ and 'data.frame':  49229 obs. of  46 variables:

 PEP             : chr 
 PN_Oper_M       : chr  
 Desig_Oper_M    : chr  
 Refer_M         : int  
 Estado_M        : chr  
 Conc_SAP        : chr  
 Inc_SAP         : chr  
 Conc_SICAP      : chr  
 Incu_SICAP      : chr  
 Avance          : chr  
 Quemado         : chr  
 RTD             : chr  
 F_Ini_Plan_     : int  20200303 20201021 20210211 20210211 20210211...
 F_Fin_Plan_     : int  20200424 20201021 20210211 20210211 20210211...
 F_Cie_Plan_     : int  20200430 20201027 20210217 20210217 20210217...
 Grupos          : chr 
 Localiza        : chr  
 Zona            : chr  
 AG              : chr  
 GNTs            : chr  
 Empresas        : chr  
 Hitos           : chr  
 Req_Pdtes       : int 
 Falta           : int  
 CADO            : chr  
 UDC             : chr  
 New             : int  
 Estr            : int  
 N_HNC           : chr  
 F_Libe_R_       : int  20191211 20200727 20201202 20201202 20201202... 
 F_Ini_R_        : int  20200303 20210308 20210216 20210204 20210218...
 F_CERR_         : int  20200430 20210323 20210305 20210305 20210316...
 F_Fin_Prod_     : int  20200424 20210316 20210216 20210204 20210218...
 F_Fin_Cal_      : int  20200429 20210318 20210222 20210204 20210313...
 Est_SICAP       : chr  
 Est_SIPLA       : chr  
 ZPLA            : int 
 ZING            : int  
 Comentarios     : chr  
 IntExt          : chr  
 Perfil          : chr  
 H_Planif        : chr  
 DOP_DI_Number   : chr  
 DOP_DI_Status   : chr  
 DOP_DI_Ubicacion: chr  
 Manual_JC       : int  

 - attr(*, ".internal.selfref")=<externalptr> 

All dates are merged as integer and I want to convert them into date. I have two questions:

 - I extract one of the columns and try to convert it with ymd() using the following code:
  
   d1 <- all[ , 30]
   d1 <- ymd(d1)

But I get the following error:

   "Warning message:
    All formats failed to parse. No formats found."

There are empty values, could it be the problem?

  • Is there a quick way to convert several columns format?? The dataframe has no headers so I have to do it calling the column position.

Many thanks Hugo

D.J
  • 1,180
  • 1
  • 8
  • 17

2 Answers2

2

I use ymd() in other contexts. I think what you want, is to define the columns as dates. Try:

df$F_Fin_Cal_ <- as.Date(df$F_Fin_Cal_, format="%Y%m%d") # df is the name of your data.frame

for all colums seperatly or with lapply() for all columns at once.

cols <- c("col1", "col2",...) # names of all relevant columns
cols <- c(1,2,3,...) # alternative adressing of columns
df[cols] <- lapply(df[cols], as.Date, format="%Y%m%d")
Clem Snide
  • 483
  • 6
  • 13
0

You could also use the lubridate package

df$F_Fin_Cal_ <- as.character(df$F_Fin_Cal_) #in case your cols are not char, convert to char
df$F_Fin_Cal_ <- lubridate::as_date(df$F_Fin_Cal_)

I convert the column to character because lubridate or even as.Date works best with char. I am not sure what the col type of your column is.

You can also use lapply like @Clem showed

Kay
  • 799
  • 1
  • 11
  • 29