error message: do not know how to convert 'dataFrame$col' to class “Date”

Jennifer B. picture Jennifer B. · Nov 10, 2017 · Viewed 20.8k times · Source

I have a data frame with records from the month of October 2017. Column 6 has the dates as a character vector.

This is what it looks like:

> october2017[1:6,1:6]
        V1        V2 V3  V4    V5        V6
1 89108060 IN0000005 P2 RK1 CA1-R 10/1/2017
2 10503818 IN0000014 P2 RK1  CA31 10/2/2017
3 89108152 765000054 P2 RK1  CA31 10/3/2017
4 89108152 765000197 P2 RK1  CA31 10/4/2017
5 89108206 200000162 P2 RK1  CA31 10/5/2017
6 89108206 100001098 P2 RK1  CA31 10/6/2017
> class(october2017$V6)
[1] "character"

The actual data frame is much larger than this. What I want to do is create a new column to denote the day of the week that matches each date and add it to the data frame. If the date is "10/1/2017" I want the new column denoting the day of the week to show "Sunday" in that row.

This is what I want the data frame to look like:

> october2017[1:6,1:7]
        V1        V2 V3  V4    V5        V6     V7
1 89108060 IN0000005 P2 RK1 CA1-R 10/1/2017 Sunday
2 10503818 IN0000014 P2 RK1  CA31 10/2/2017 Monday
3 89108152 765000054 P2 RK1  CA31 10/3/2017 Tuesday
4 89108152 765000197 P2 RK1  CA31 10/4/2017 Wednesday
5 89108206 200000162 P2 RK1  CA31 10/5/2017 Thursday
6 89108206 100001098 P2 RK1  CA31 10/6/2017 Friday

This is what I tried: newcol = weekdays(as.Date(october2017$v6, format="%m/%d/%Y")) october2017 = cbind(october2017,newcol, stringsAsFactors=FALSE)

This is the error message I get when I try to run the first line of this code: Error in as.Date.default(october2017$v6, format = "%m/%d/%Y") : do not know how to convert 'october2017$v6' to class “Date”

Can anyone help me understand why this is happening?

Answer

r2evans picture r2evans · Nov 10, 2017

as.Date is a function that uses S3 method dispatch. That is, there are actually several functions:

methods("as.Date")
# [1] as.Date.character as.Date.date      as.Date.dates     as.Date.default  
# [5] as.Date.factor    as.Date.numeric   as.Date.POSIXct   as.Date.POSIXlt  
# see '?methods' for accessing help and source code

When you call as.Date(x), R looks at the class of the first object and uses the appropriate S3 method. If none is found and a .default function exists, then that will be used as a "last resort".

If you look at the source for each of the methods, you will only find the string "do not know how to convert" in as.Date.default:

as.Date.default
# function (x, ...) 
# {
#     if (inherits(x, "Date")) 
#         return(x)
#     if (is.logical(x) && all(is.na(x))) 
#         return(structure(as.numeric(x), class = "Date"))
#     stop(gettextf("do not know how to convert '%s' to class %s", 
#         deparse(substitute(x)), dQuote("Date")), domain = NA)
# }

If it were one of the known classes (character, date, dates, factor, numeric, POSIXct, or POSIXlt, and now also not Date or logical-NA), then it would have run the specific function instead (none of which include that error string). This suggests that your $v6 column is a different class. Without an MWE, it is complete speculation.

I suggest you find the actual class of your data

class(dataFrame$v6)

and figure out how to convert it to one of the known versions.

Edit

Furthermore, note that R is case-sensitive. Your MWE uses lower-case v6 but your column names are upper-case. How about just

october2017$V7 <- weekdays(as.Date(oct$V6, format="%m/%d/%Y"))

When you look at october2017$v6 (lower-case), it returns NULL, which triggers the .default method of as.Date.