Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable data.table names at the bottom?

I am running Rstudio 0.99.489 and R-3.2.3 on Windows 7

How can I avoid printing V1 and N at the bottom of the data ?

options(datatable.print.nrows = Inf)
dt <- data.table(sample.int(2e3, 1e4, T))
print(dt[ , .(.N), V1])

...
1980:  419  1
1981:  898  2
1982: 1260  1
        V1  N
like image 950
Oleg Bondar Avatar asked Mar 14 '23 18:03

Oleg Bondar


2 Answers

You can manipulate print of an object as a regular character vector.

library(data.table)
options(datatable.print.nrows = Inf)
dt = data.table(sample.int(2e3, 1e4, T))
myprint = function(x){
    prnt = capture.output(print(x))
    cat(prnt[-length(prnt)], sep="\n")
}
myprint(dt[ , .(.N), V1])
like image 148
jangorecki Avatar answered Mar 16 '23 17:03

jangorecki


Here's an alternative to consider. Since data.tables are enhanced data.frames, why not just use the print method for data.frames? That way, you get both printing all the rows of the inputs, but without the column names appearing at the bottom as well.

For instance, the following dataset is sufficient to demonstrate the behavior of printing names at the bottom.

set.seed(1)
dt <- data.table(sample(21, 1000, TRUE)) ## Sufficient to demonstrate behavior
dt[, .N, by = V1]                        ## Shows the names at the bottom

You can manually specify the print.data.frame method, like this:

print.data.frame(dt[, .N, by = V1])      ## Specify use of data.frame print method

Or, since you're not printing something that affects your original data.table, you could also do something like this:

setDF(dt[, .N, by = V1])[]                ## dt stays a `data.table`
like image 35
A5C1D2H2I1M1N2O1R2T1 Avatar answered Mar 16 '23 17:03

A5C1D2H2I1M1N2O1R2T1