gpt4 book ai didi

r - 打印包含 S4 对象列表列的 data.frame

转载 作者:行者123 更新时间:2023-12-04 04:29:50 25 4
gpt4 key购买 nike

打印 data.frame 是否存在一般问题?当它具有 S4 对象的列表列时?还是我只是运气不好?

我用来自 git2r 的对象遇到了这个问题包,但维护者 Stefan Widgren 从 Matrix 中指出了这个例子以及。我注意到如果通过 dplyr::tbl_df() 发送该对象可以打印.我接受打印并没有提供关于 S4 对象的太多信息;我只问没有错误。

更新了更高的野心:可以data.frame - 类似的质量得到保留?

library(Matrix)
library(dplyr)
m <- new("dgCMatrix")
isS4(m)
#> [1] TRUE
df <- data.frame(id = 1:2)
df$matrices <- list(m, m)
df
#> Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L, : first argument must be atomic
tbl_df(df)
#> Source: local data frame [2 x 2]
#>
#> id
#> (int)
#> 1 1
#> 2 2
#> Variables not shown: matrices (list).

## force dplyr to show the tricky column
tbl_df(select(df, matrices))
#> Source: local data frame [2 x 1]
#>
#> matrices
#> (list)
#> 1 <S4:dgCMatrix, CsparseMatrix, dsparseMatrix, generalMatrix, dCsparseMatrix,
#> 2 <S4:dgCMatrix, CsparseMatrix, dsparseMatrix, generalMatrix, dCsparseMatrix,

## rawr points out that this does not error ... but loses the df quality
print.default(df)
#> $id
#> [1] 1 2
#>
#> $matrices
#> $matrices[[1]]
#> 0 x 0 sparse Matrix of class "dgCMatrix"
#> <0 x 0 matrix>
#>
#> $matrices[[2]]
#> 0 x 0 sparse Matrix of class "dgCMatrix"
#> <0 x 0 matrix>
#>
#>
#> attr(,"class")
#> [1] "data.frame"

最佳答案

另一种选择(可能产生比预期更大的后果)是:

library(Matrix)

format.list <- function(x, ...) { rep(class(x[[1]]), length(x)) }

m <- new("dgCMatrix")
df <- data.frame(id = 1:2)
df$matrices <- list(m, m)
df

## id matrices
## 1 1 dgCMatrix
## 2 2 dgCMatrix

关于r - 打印包含 S4 对象列表列的 data.frame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34731771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com