Hi there,
Thank you for your time in advanced.
I am confronted with a Tibble imported with read_csv() which contains columns with dates that were considered as characters, but are in fact dates.
I believe R has to recognize these as dates to perform date range filters, therefore I must parse these columns into the correct data type which is date. (Is that correct or as characters R understands it? In all cases, this must be a "good practice" kind of thing right?)
The tibble dates are formated as such:
"16-Jan-17" "2-Mar-17" "4-Mar-17" "9-Feb-17" "8-Mar-17"
I could just repeat this for every column:
parse_date(df_all$`Origin ETD`, "%d-%b-%y")
[1] "2017-01-16" "2017-03-02" "2017-03-04"
I was looking to create a more elegant solution, I thought lapply() could be used with parse_date().
First I thought of creating a list which I could store temporarily each column together, so after identifying these columns I would save them in a list.
x <- list(df_all$`Origin ETD`,df_all$`Destination ETA`, df_all$`Added`,
df_all$`First Load`, df_all$`Last Discharge`, df_all$`ETD First Load`,
df_all$`ETA Last Discharge` ,df_all$`Discharge`,
df_all$`ETD Load`, df_all$`ETA Discharge`)
lapply(x, parse_date(x, "%d-%b-%y"))
My return was:
Error in parse_vector(x, col_date(format), na = na, locale = locale, trim_ws = trim_ws) :
is.character(x) is not TRUE
What would be the best way to do this without just repeating the code and changing the syntactical column name for each case?
I'm learning dplyr and would be interesting to have a base R approach and dplyr approach for learning purposes.
Another approach would be to make my own function, but it is not clear for me how to make a FOR loop which would cycle through syntactical column names.
If I could handle the syntactical column names I believe the for loop would look something like:
x <- c(`Origin ETD`,`Destination ETA`, `Added`, `First Load`, `Last Discharge`,
`ETD First Load`,
`ETA Last Discharge` ,`Discharge`, `ETD Load`, `ETA Discharge`)
parse_colums <- function(x){
for (i in seq_along(x)) {
parse_date(x[i], "%d-%b-%y")
}}
I believe I am somewhat on the right track... ish...
I would greatly appreciate feedback as I try to sharpen my R skills.
Best regards,
LF.