-->

我怎样才能读取摘要()对数据帧的代码?(How can I read the code for su

2019-06-25 16:47发布

我有一个数据帧,我想学习总结如何生成它的信息。 具体而言,如何摘要生成用于在因素的每个级别的元素数的计数。 我可以使用总结,但我想学习如何使用因素更好地工作。 当我尝试?总之,我刚刚得到的一般信息。 这是不可能的,因为它是在字节码?

Answer 1:

What we see when you type summary is

> summary
function (object, ...) 
UseMethod("summary")
<bytecode: 0x0456f73c>
<environment: namespace:base>

This is telling us that summary is a generic function and has many methods attached to it. To see what those methods are actually called we can try

> methods(summary)
 [1] summary.aov             summary.aovlist         summary.aspell*        
 [4] summary.connection      summary.data.frame      summary.Date           
 [7] summary.default         summary.ecdf*           summary.factor         
[10] summary.glm             summary.infl            summary.lm             
[13] summary.loess*          summary.manova          summary.matrix         
[16] summary.mlm             summary.nls*            summary.packageStatus* 
[19] summary.PDF_Dictionary* summary.PDF_Stream*     summary.POSIXct        
[22] summary.POSIXlt         summary.ppr*            summary.prcomp*        
[25] summary.princomp*       summary.srcfile         summary.srcref         
[28] summary.stepfun         summary.stl*            summary.table          
[31] summary.tukeysmooth*   

   Non-visible functions are asterisked

Here we see all the methods associated with the summary function. What this means is that there is different code for when you call summary on an lm object than there is when you call summary on a data.frame. This is good because we wouldn't expect the summary to be conducted the same way for those two objects.

To see the code that is run when you call summary on a data.frame you can just type

summary.data.frame

as shown in the methods list. You'll be able to examine it and study it and do whatever you want with the printed code. You mentioned that you were interested in factors so you will probably want to examine the output of summary.factor. Now you might notice that some of the methods printed had an asterisk (*) next to them which implies that they're non-visible. This essentially means that you can't just type the name of the function to try to view the code.

> summary.prcomp
Error: object 'summary.prcomp' not found

However, if you're determined to see what the code actually is you can use the getAnywhere function to view it.

> getAnywhere(summary.prcomp)
A single object matching ‘summary.prcomp’ was found
It was found in the following places
  registered S3 method for summary from namespace stats
  namespace:stats
with value

function (object, ...) 
{
    vars <- object$sdev^2
    vars <- vars/sum(vars)
    importance <- rbind(`Standard deviation` = object$sdev, `Proportion of Variance` = round(vars, 
        5), `Cumulative Proportion` = round(cumsum(vars), 5))
    colnames(importance) <- colnames(object$rotation)
    object$importance <- importance
    class(object) <- "summary.prcomp"
    object
}
<bytecode: 0x03e15d54>
<environment: namespace:stats>

Hopefully this helps you explore the code in R much more easily in the future.

For even more details you can view Volume 6/4 of The R Journal (warning, pdf) and read Uwe Ligge's "R Help Desk" section which deals with viewing the source code of R functions.



文章来源: How can I read the code for summary() for a data frame?
标签: r r-faq