【问题标题】:Produce a graphic tree diagram showing the structure of an R object生成显示 R 对象结构的图形树形图
【发布时间】:2017-10-06 13:11:07
【问题描述】:

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

例如,对于这个例子,

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 获得的文件系统中的文件夹:

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

结果可以是图形字符,如tree,也可以是如上所示的实际图形。有这样的东西吗?

【问题讨论】:

标签: r object tree diagram


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2022-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多