Reputation: 11
I have calculated ratios for a few variables using svyratio, but it doesn't let me convert the return value into a dataframe. Using DF1 <- as.data.table(loc_ratio), it gives the error
"Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ‘"svyratio"’ to a data.frame"
Upvotes: 0
Views: 498
Reputation: 263332
There is basically no default method for as.data.frame. What there is only the warning you see:
as.data.frame.default
#====== console output=====
function (x, ...)
stop(gettextf("cannot coerce class %s to a data.frame", sQuote(deparse(class(x))[1L])),
domain = NA)
<bytecode: 0x55e6ce5e1ff0>
<environment: namespace:base>
So unless the author of package::survey
has written an as method, you will see that message. If you know for a fact that the contents of your svyratio value should be a two element list then you might try adding list to the objects class attribute. The x
-object was built using the first example in the ?svyratio
help page.
class(x) <- list("svyratio", "list")
> as.data.frame(x)
arrests arrests.1
alive 0.1535064 5.770992e-05
In this case the svyratio object was a list of two matrices, but since they only had one element each, the coercion proceeded:
> str(x)
List of 2
$ ratio: num [1, 1] 0.154
..- attr(*, "dimnames")=List of 2
.. ..$ : chr "alive"
.. ..$ : chr "arrests"
$ var : num [1, 1] 5.77e-05
..- attr(*, "dimnames")=List of 2
.. ..$ : chr "alive"
.. ..$ : chr "arrests"
- attr(*, "call")= language svyratio.survey.design2(~alive, ~arrests, design = scddes)
- attr(*, "class")= chr [1:2] "svyratio" "list"
Upvotes: 1