简体   繁体   English

使用R将异常类强制为数据帧

[英]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. 我正在使用R包mc2d,最终输出是类'mc'的对象,该类对于此包是唯一的。 I would like to render the results of summary.mc in a nice table using knitr; 我想使用knitr在一个漂亮的表中呈现summary.mc的结果; however, aside from printing a summary of the 'mc' object, I cannot figure out how to use this function. 但是,除了打印“ mc”对象的摘要之外,我无法弄清楚如何使用此功能。 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. 我尝试在mc2d :: summary.mc上阅读源代码,以阐明该函数如何访问这些属性的信息,但阅读代码超出了我当前的能力范围。

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. 如果str(summary(fake.Model))的输出是仅包含一个具有规则结构的元素的列表,如[1:101, 1:101, 1] -0.0379 0.6593 0.2933 1.4019 -0.126 ... ..暗示它只是一个矩阵,然后as.data.frame(summary(fake.Model)[[1]])应该从矩阵创建一个数据帧。

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. 您应该使用unmc函数unmc (x,drop = TRUE)转换mcobject法线数组。 Then you can add it to the data.frame function. 然后,您可以将其添加到data.frame函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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