生成显示R对象结构的图形树图

编程入门 行业动态 更新时间:2024-10-19 13:26:35
本文介绍了生成显示R对象结构的图形树图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在R中, str()可方便地显示对象的结构,例如 lm()和其他建模函数返回的列表的列表,但是它让出了太多的输出.我正在寻找一种工具来创建简单的树形图,该树形图仅显示列表元素的名称及其结构.

In R, str() is handy for showing the structure of an object, such as the list of lists returned by lm() and other modelling functions, but it gives way too much output. I'm looking for some tool to create a simple tree diagram showing only the names of the list elements and their structure.

例如,对于此示例

data(Prestige, package="car") out <- lm(prestige ~ income+education+women, data=Prestige) str(out, max.level=2) #> List of 12 #> $ coefficients : Named num [1:4] -6.79433 0.00131 4.18664 -0.00891 #> ..- attr(*, "names")= chr [1:4] "(Intercept)" "income" "education" "women" #> $ residuals : Named num [1:102] 4.58 -9.39 4.69 4.22 8.15 ... #> ..- attr(*, "names")= chr [1:102] "gov.administrators" "general.managers" "accountants" "purchasing.officers" ... #> $ effects : Named num [1:102] -472.99 -123.61 -92.61 -2.3 6.83 ... #> ..- attr(*, "names")= chr [1:102] "(Intercept)" "income" "education" "women" ... #> $ rank : int 4 #> $ fitted.values: Named num [1:102] 64.2 78.5 58.7 52.6 65.3 ... #> ..- attr(*, "names")= chr [1:102] "gov.administrators" "general.managers" "accountants" "purchasing.officers" ... #> $ assign : int [1:4] 0 1 2 3 #> $ qr :List of 5 #> ..$ qr : num [1:102, 1:4] -10.1 0.099 0.099 0.099 0.099 ... #> .. ..- attr(*, "dimnames")=List of 2 #> .. ..- attr(*, "assign")= int [1:4] 0 1 2 3 #> ..$ qraux: num [1:4] 1.1 1.44 1.06 1.06 #> ..$ pivot: int [1:4] 1 2 3 4 #> ..$ tol : num 1e-07 #> ..$ rank : int 4 #> ..- attr(*, "class")= chr "qr" #> $ df.residual : int 98 ...

我想得到这样的东西:

这类似于我从 tree 中获得的文件系统文件夹的信息:

This is similar to what I get from tree for file folders in my file system:

C:\Dropbox\Documents\images>tree Folder PATH listing Volume serial number is 2250-8E6F C:. +---cartoons +---chevaliers +---icons +---milestones +---minard +---minard-besancon

结果可以是图形字符(如 tree 中的图形字符)或实际的图形,如上所示.有这样的东西吗?

The result could be either in graphic characters, as in tree or an actual graphic as shown above. Is anything like this available?

推荐答案

从 str 输出中获取此信息的简单方法类似于...

A simple approach to getting this from the str output would be something like...

a <- capture.output(str(out, max.level=2)) a <- trimws(gsub("\\:.*", "", a[grepl("\\$", a)])) cat(a, sep="\n") $ coefficients $ residuals $ effects $ rank $ fitted.values $ assign $ qr ..$ qr ..$ qraux ..$ pivot ..$ tol ..$ rank $ df.residual $ xlevels $ call $ terms $ model ..$ prestige ..$ income ..$ education ..$ women

更多推荐

生成显示R对象结构的图形树图

本文发布于:2023-07-18 12:50:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图形   对象   结构

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!