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

删除回忆录丶 提交于 2019-12-20 09:26:01

问题


The data.table has a nice feature that suppresses output to the head and tail of the table.

Is it possible to view / print more than 100 rows at once?

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 (e.g. 200 rows) that I just want to view.


回答1:


The print method of data.table has an argument 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



回答2:


View() (as in View(iris) or View(dtIris[1:120,])) doesn't truncate data.tables, and can often be nicer than printing/spewing out a data.* to the console.




回答3:


To print the top 60 and bottom 60 lines (default is top 5 and bottom 5):

print(dtIris, topn = 60)



回答4:


A messy option, but you could always export it into excel to view it with excels convenience.

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



回答5:


You could convert it to a data.frame only for printing:

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


来源:https://stackoverflow.com/questions/12162657/possible-to-print-more-than-100-rows-of-a-data-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!