I am wondering why this error occurs. I would like to convert this using brackets as I am making sequential conversions in a loop. And because I just want to be able to do it and understand what is happening.
head(clean.deposit.rates)
Date
1 1/31/1983
2 2/28/1983
3 3/31/1983
4 4/30/1983
5 5/31/1983
6 6/30/1983
class(clean.deposit.rates)
[1] "data.frame"
class(as.Date(clean.deposit.rates[[1]], "%m/%d/%Y"))
[1] "Date"
class(as.Date(clean.deposit.rates$Date, "%m/%d/%Y"))
[1] "Date"
as.Date(clean.deposit.rates["Date"], "%m/%d/%Y")
Error in as.Date.default(clean.deposit.rates["Date"], "%m/%d/%Y") :
do not know how to convert 'clean.deposit.rates["Date"]' to class “Date”
You need to use two
[brackets. With one, the column remains as a data frame. With two, it becomes an atomic vector which can properly be passed to the correctas.DatemethodSince
df["Date"]is classdata.frame, thexargument usesas.Date.defaultbecause there is noas.Date.data.framemethod. The error is triggered becausexisFALSEfor all theifstatements and continues throughas.Date.defaultto the lineUsing
df[["Date"]], the column becomes a vector and is passed to eitheras.Date.characteroras.Date.factordepending on the class of the vector, and the desired result is returned.