简体   繁体   English

可以打印超过 100 行的 data.table 吗?

[英]Possible to print more than 100 rows of a data.table?

The data.table has a nice feature that suppresses output to the head and tail of the table. data.table 有一个很好的特性,可以抑制表头和表尾的输出。

Is it possible to view / print more than 100 rows at once?是否可以一次查看/打印超过 100 行?

library(data.table)
## Convert the ubiquitous "iris" data to a data.table
dtIris = as.data.table(iris)
## Printing 100 rows is possible
dtIris[1:100, ]
## Printing 101 rows is truncated
dtIris[1:101, ]

I often have data.table results that are somewhat large (eg 200 rows) that I just want to view.我经常有我只想查看的有点大(例如 200 行)的 data.table 结果。

The print method of data.table has an argument nrows : data.table的 print 方法有一个参数nrows

args(data.table:::print.data.table)
function (x, nrows = 100L, digits = NULL, ...) 

You can use this to control how many rows get printed:您可以使用它来控制打印的行数:

print(dtIris, nrow=105)
.....
99:          5.1         2.5          3.0         1.1 versicolor
100:          5.7         2.8          4.1         1.3 versicolor
101:          6.3         3.3          6.0         2.5  virginica
102:          5.8         2.7          5.1         1.9  virginica
103:          7.1         3.0          5.9         2.1  virginica
104:          6.3         2.9          5.6         1.8  virginica
105:          6.5         3.0          5.8         2.2  virginica
     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species

View() (如在View(iris)View(dtIris[1:120,])中)不会截断data.table ,并且通常比打印/喷出data.*到控制台更好。

打印前 60 行和后 60 行(默认为前 5 行和后 5 行):

print(dtIris, topn = 60)

You could convert it to a data.frame only for printing:您可以将其转换为仅用于打印的 data.frame:

iris_dt = as.data.table(iris)
print(as.data.frame(iris_dt))

A messy option, but you could always export it into excel to view it with excels convenience.一个凌乱的选项,但您始终可以将其导出到 excel 中以方便地查看它。

library(xlsReadWrite)
write.xls(mydata, "c:/mydata.xls")

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

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