简体   繁体   中英

Coercing an unusual class into a data frame with R

I am working with the R package mc2d and a final output is an object of class 'mc', which is unique to this package. I would like to render the results of summary.mc in a nice table using knitr; however, aside from printing a summary of the 'mc' object, I cannot figure out how to use this function. I cannot coerce it to a data frame; it throws the error:

Error in as.data.frame.default(Model) : cannot coerce class ""mc"" to a data.frame

This is an example of the output I would like to put into a nicer format:

> summary(Model$Risk)
node :

        mean    sd       Min      2.5%     25%      50%      75%      97.5%     
median 5.77e-05 7.46e-04 0.00e+00 2.08e-13 1.97e-09 5.74e-08 1.09e-06 1.77e-04
mean   2.94e-03 8.94e-03 5.59e-12 1.42e-08 5.92e-05 1.39e-03 4.55e-03 1.09e-02
2.5%   6.06e-08 6.04e-07 0.00e+00 0.00e+00 1.96e-12 6.25e-11 1.29e-09 2.22e-07
97.5%  7.58e-03 6.07e-02 3.77e-15 1.20e-10 8.15e-07 1.77e-05 2.94e-04 4.01e-02

I cannot just reference the vectors with the $ operator and find the quartiles myself, because some rows relate to uncertainty and others to variability, and I cannot tell which is which on my own. I tried reading the source at mc2d::summary.mc to shed some light on how the function accesses these attributes, but reading the code was beyond my current abilities.

I am sure there are a lot of unusual classes out there that cannot be brute forced into a data frame - so in general, what do people do?

Minimal reproducible example:

library(mc2d)
ndvar(101) #setting number of variability dimensions
ndunc(101) #uncertainty dimensions
fake.mean <- mcstoc(runif, min=0, max=2, type='U') #'uncertain' parameter estimates
fake.sd <- mcstoc(runif, min=0.1, max=1.5, type='U') 

fake.data <- mcstoc(rnorm, mean=fake.mean, sd=fake.sd, type='VU') #incorporating uncertain parameters and variable data

fake.Model <- mc(fake.data) #creating mc object
summary(fake.Model) 

If the output of str(summary(fake.Model)) is a list with only one element that has a regular structure as indicated by [1:101, 1:101, 1] -0.0379 0.6593 0.2933 1.4019 -0.126 ... .. which suggests its just a matrix then as.data.frame( summary(fake.Model)[[1]]) should create a dataframe from the matrix.

The general principle is to see what the object contains and then use "[" or "[[" to extract the items you need. The output from summary methods is generally a list and you may find further value in looking at the print method for the summary object, since sometimes the print method will construct other sorts of output beyond what is in the summary elements.

You should use unmc function unmc (x,drop = TRUE) to convert the mcobject normal array. Then you can add it to the data.frame function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM