Bug 15997 - Calling as.data.frame with noquoted character vectors throws "cannot coerce" error
Summary: Calling as.data.frame with noquoted character vectors throws "cannot coerce" ...
Alias: None
Product: R
Classification: Unclassified
Component: Misc (show other bugs)
Version: R-devel (trunk)
Hardware: All All
: P5 trivial
Assignee: R-core
Depends on:
Reported: 2014-09-28 08:21 UTC by Richard Cotton
Modified: 2014-11-04 13:44 UTC (History)
1 user (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Richard Cotton 2014-09-28 08:21:09 UTC
When trying to use a noquoted character vector as a column of a data frame, I receive an error stating "cannot coerce class ""noquote"" to a data.frame".

To reproduce, try:

data.frame(x = noquote(letters))
## Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
##   cannot coerce class ""noquote"" to a data.frame

This is a specific case of the more general problem of trying to include an atomic vector with a class attribute into a data frame.

data.frame(x = structure(1:10, class = "something"))
## Error in as.data.frame.default(x[[i]], optional = TRUE) : 
##   cannot coerce class ""something"" to a data.frame

Currently the base package has as.data.frame methods for including factors, Dates and POSIXcts, all of which are the same function:

identical(as.data.frame.POSIXct, as.data.frame.Date)   #TRUE
identical(as.data.frame.POSIXct, as.data.frame.factor) #TRUE

Thus the simplest workaround is to reuse that function for noquotes. 

as.data.frame.noquote <- base::as.data.frame.factor
data.frame(x = noquote(letters)) #now works as expected
Comment 1 Duncan Murdoch 2014-11-04 13:44:42 UTC
I'll add an as.data.frame.noquote method, so this will be fixed.  I'll also add a line to the ?as.data.frame help topic explaining how to do this for other vector-like classes.