How to avoid printing line numbers using data.table?

Printing a data.tableadds a number for each line of output to 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 "1:", "2:", "3:" before the data rows? I would like to write html- or latex-formatted output of my data using xtable.

+4
source share
2 answers

print.data.tablehas an argument row.namesthat is set to TRUE by default. Just use

> print(DT, row.names=FALSE)
 V1 V2
  1  3
  2  2
  3  1
+6
source

If you want to suppress the names in xtable(), you must use the arguments 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}

, data.table data.frame

+3

Source: https://habr.com/ru/post/1535697/


All Articles