Desire to convert class of multi cols without knowing the location .
This is the dataset
# Dataset name call : df . # It is a example , real data has many columns
# that you cannot have a clear index by one sight.
A.Date Price B.Date C.Date Size D.Date
2017-01-01 502 2017-01-03 2017-11-01 45.4 2016-10-01
2015-01-31 602 2017-02-03 2013-07-11 65.4 2016-03-24
I have a code like below :
df[,grepl("Date",colnames(df))] <-
lapply(df[,grepl("Date",colnames(df))],function(x) as.Date(x))
But its turn out error :
Error in strptime(x, f) : input string is too long
Even i tried this code :
DateCol <- grep("Date",names(df))
df[,c(DateCol)] <- as.Date(df[,c(DateCol)])
It goes by error again like Error in as.Date.default(df[, c(DateCol)]) : 'df[, c(DateCol)]' class “Date” cannot be defined
What is wrong with the code and what is the solution ?