Skip to content

how to print a data.frame without row.names

6 messages · Heinz Tuechler, Romain Francois, Martin Maechler +1 more

#
Dear All,
is there a simple way to print a data.frame without its row.names?

example:
datum <- as.Date(c("2004-01-01", "2004-01-06", "2004-04-12"))
content <- c('Neujahr', 'Hl 3 K.', 'Ostern')
df1 <- data.frame(datum, content)
print(df1)

       datum content
1 2004-01-01 Neujahr
2 2004-01-06 Hl 3 K.
3 2004-04-12  Ostern

Can I get this "table" without 1, 2, 3 ?

Thanks in advance

Heinz Tuechler
#
Le 02.08.2005 15:45, Heinz Tuechler a ??crit :
See write.table and its row.names argument

R> write.table(df1, row.names=FALSE)

Romain
#
At 16:05 02.08.2005 +0200, Romain Francois wrote:
write.table(df1, row.names=FALSE, quote=FALSE)
datum content
2004-01-01 Neujahr
2004-01-06 Hl 3 K.
2004-04-12 Ostern

I tried this, but then the column headers and column contents are not aligned.

If you expand the example, you see clearly the difference.

datum <- as.Date(c("2004-01-01", "2004-01-06", "2004-04-12"))
content <- c('Neujahr', 'Hl 3 K.', 'Ostern')
number <- c(1, 6, 110)
string <- c('a', 'bbbb', 'c')
df1 <- data.frame(datum, content, number, string)
print(df1)
       datum content number string
1 2004-01-01 Neujahr      1      a
2 2004-01-06 Hl 3 K.      6   bbbb
3 2004-04-12  Ostern    110      c

write.table(df1, row.names=FALSE, quote=FALSE)
datum content number string
2004-01-01 Neujahr 1 a
2004-01-06 Hl 3 K. 6 bbbb
2004-04-12 Ostern 110 c

Maybe I missed a function like print.xtable with type="ascii". It seems
that it has to be done with cat.

Thank you

Heinz
#
.......................

    Heinz> I tried this, but then the column headers and column
    Heinz> contents are not aligned.

  ........................

Use the tabulator if you need them aligned :

write.table(USArrests, row.names = FALSE, sep = "\t")
#
Martin Maechler <maechler at stat.math.ethz.ch> writes:
Unless a column or a header is 8 chars or wider (and UrbanPop is!).

This seems to do it:

  x <- as.matrix(format(USArrests))
  rownames(x) <- rep("", nrow(x))
  print(x, quote=FALSE, right=TRUE)
#
Thanks to all of you for your help.

As far as I see, the solution of Peter Dalgaard works exactly as I want.
All other solutions have limitations.

Heinz
At 19:19 02.08.2005 +0200, Peter Dalgaard wrote: