Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid printing line numbers with data.table?

Tags:

r

data.table

Printing a data.table adds a number for each line of output before the actual output, e.g.

> DT <- data.table(cbind(1:3,3:1))
> DT
    V1 V2
 1:  1  3
 2:  2  2
 3:  3  1
> print(DT)
    V1 V2
 1:  1  3
 2:  2  2
 3:  3  1

How can I avoid printing the "1:", "2:", "3:" before the data rows? I would like write html- or latex-formated output of my data using xtable.

like image 216
Stefan Avatar asked Apr 09 '14 08:04

Stefan


2 Answers

print.data.table has an argument row.names, which is set to TRUE by default. Just use

> print(DT, row.names=FALSE)
 V1 V2
  1  3
  2  2
  3  1
like image 111
shadow Avatar answered Oct 30 '22 06:10

shadow


If you want to supress the names in xtable() output, you need to use the arguments to print.xtable():

library(xtable)

df <- data.frame(x = 1:3, y = 3:1)
xtable(df)

## % latex table generated in R 3.0.2 by xtable 1.7-1 package
## % Wed Apr  9 06:53:55 2014
## \begin{table}[ht]
## \centering
## \begin{tabular}{rrr}
##   \hline
##  & x & y \\ 
##   \hline
## 1 &   1 &   3 \\ 
##   2 &   2 &   2 \\ 
##   3 &   3 &   1 \\ 
##    \hline
## \end{tabular}
## \end{table}

print(xtable(df), include.rownames = FALSE)

## % latex table generated in R 3.0.2 by xtable 1.7-1 package
## % Wed Apr  9 06:53:55 2014
## \begin{table}[ht]
## \centering
## \begin{tabular}{rr}
##   \hline
## x & y \\ 
##   \hline
##   1 &   3 \\ 
##     2 &   2 \\ 
##     3 &   1 \\ 
##    \hline
## \end{tabular}
## \end{table}

In this case it doesn't matter that you're using a data.table instead of a data.frame

like image 22
hadley Avatar answered Oct 30 '22 06:10

hadley