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

45,713

Solution 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

Solution 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.

Solution 3

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

print(dtIris, topn = 60)
Share:
45,713
geneorama
Author by

geneorama

Updated on May 23, 2020

Comments

  • geneorama
    geneorama about 4 years

    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.

  • Roland
    Roland almost 12 years
    data.tables are used for huge amounts of data. Have fun with Excel.
  • Matt Dowle
    Matt Dowle almost 12 years
    +1 Which is also FAQ 2.11. Note also that, oddly, typing print(DT) at the prompt (with or without nrows) is faster than typing just DT. It seems to be down to R copying the whole object in the second (more common) case (during dispatch?) before the data.table method comes along to print the head and tail. If anyone knows why R does that I'd love to know. See comments in FR#1001 REPL print copy about applyClosure.
  • Timothy Alston
    Timothy Alston almost 12 years
    hence I put "a messy option". Depends on the size of your data.
  • Roland
    Roland almost 12 years
    There is messy and then there is O_O
  • Roland
    Roland almost 12 years
    If you are already working with R and your solution to a specific problem is to export the data to Excel, please ask on Stack Overflow for a better way.
  • geneorama
    geneorama almost 12 years
    I have to say, this is probably the most effective answer. However, it's not a "right" answer in terms of data.table and R. The only reason that I'm not exporting to Excel in this case is that I'm working on a server. It's almost exactly what I do for these summaries that are in the 200-500 row range.
  • geneorama
    geneorama almost 12 years
    @Roland To be fair, I was asking about printing small data.tables to the console, which is also a size that Excel can handle.
  • geneorama
    geneorama almost 12 years
    Timothy, you may find this useful: stackoverflow.com/questions/12164897/…
  • geneorama
    geneorama almost 12 years
    Very nice! Even works in the RStudio server environment, and lets me copy a little report into Excel for posterity.
  • Josh O'Brien
    Josh O'Brien almost 12 years
    @geneorama -- Thanks for adding that note. I usually work from vanilla Windows R gui or a Windows emacs installation and wondered how broadly View() is implemented. Am especially curious what it produces on a *NIX machine.
  • Roland
    Roland almost 12 years
    @geneorama Why do you use data.table if your data size is so small? data.table is optimized for large data sets. For small data sets you should just use base R, i.e., a data.frame.
  • geneorama
    geneorama almost 12 years
    @Roland The results from a data table query are also a data table, and query results are not that large. However sometimes they're just large enough that the full result will not print to my screen.
  • nsheff
    nsheff almost 10 years
    It works in linux, too, here... but I don't see how to copy a report.
  • geneorama
    geneorama over 8 years
    Excellent point. You can also set this in options, with options(datatable.print.topn=60). Also, I've learned of a feature that allows you to expand or limit the width of the printed column options(datatable.prettyprint.char=80L). This prettyprint option is not set by default, so you have to know the command in order to use it (whereas you can search options() for other options like datatable.print.topn and datatable.print.nrows
  • geneorama
    geneorama over 8 years
    Over the years I've found that at some point in nearly every project I end up exporting a lot of data to Excel to debug, and I find things that I would have otherwise missed.
  • user3032689
    user3032689 almost 8 years
    nrows does not work in my case, it displays the truncated table only. However, topn works. It's weird. I used e.g. data.table:::print.data.table(dtIris,nrows = 100)
  • sautedman
    sautedman over 7 years
    In my case n works, but neither nrows nor topn works (R 3.3.2)
  • geneorama
    geneorama over 6 years
    Good workaround, but it doesn't answer the question. If you start looking at workarounds then you could suggest a lot of things, like "just use SAS!" or "print each row one at a time!"... you get the idea.
  • Reilstein
    Reilstein over 6 years
    print(DT, topn=150) works for me to display the first 150 rows. R version 3.4.2 (2017-09-28) data.table_1.10.4-3 Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 16.04.3 LTS