41

I would like to print a list in R without line numbers.

I tried the cat command but it doesn't work for lists.

Does anyone have any suggestions ?

    GROUP    SEX INCOME STATE    n  mean
11       1   Male      1    AL  159 26.49
12       2 Female      1    AL  204 26.64
13       3   Male      2    AL  255 27.97
14       4 Female      2    AL  476 29.06

Example data to use:

foo <- structure(list(GROUP = 1:4, 
                      SEX = structure(c(2L, 1L, 2L, 1L),
                                      .Label = c("Female", "Male"),
                                      class = "factor"),
                      INCOME = c(1L, 1L, 2L, 2L), 
                      STATE = structure(c(1L, 1L, 1L, 1L), .Label = "AL", 
                                        class = "factor"), 
                      n = c(159L, 204L, 255L, 476L), 
                      mean = c(26.49, 26.64, 27.97, 29.06)),
                 .Names = c("GROUP", "SEX", "INCOME", "STATE", "n", "mean"), 
                 class = "data.frame", row.names = 11:14)
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
pmagunia
  • 1,718
  • 1
  • 22
  • 33
  • Can you give an example of you code or what you are trying to display? –  Sep 19 '13 at 20:52
  • 1
    Whilst we are waiting, and to clarify matters, what you show is (or looks like) a data frame, not a list. Is it the `11, 12, 13, 14` that you don't want printing? Also, to help out the [so] crowd, could you include the output of `dput(foo)` in your question, after the example data?, where `foo` is the R object you pasted in? That will give something concrete to work with. – Gavin Simpson Sep 19 '13 at 21:16
  • @GavinSimpson For privacy with this particular data set I'd rather not print out the whole data set. Will use dput in the future though – pmagunia Sep 19 '13 at 21:24
  • @pmagunia I meant just for the snippet you showed. For example, if your object is `foo` and you want only the first 4 rows, then `dput(foo[1:4, ])` would help. As it happens I did it for you by reading the four lines into R, changing the rownames, and `dput()` it. – Gavin Simpson Sep 19 '13 at 21:27

2 Answers2

52

Do you just want the argument row.names = FALSE? E.g.

> print(foo, row.names = FALSE)
 GROUP    SEX INCOME STATE   n  mean
     1   Male      1    AL 159 26.49
     2 Female      1    AL 204 26.64
     3   Male      2    AL 255 27.97
     4 Female      2    AL 476 29.06

where this is using the ?print.data.frame method.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • 6
    Good one, did not know about that option :) Much better than my solution – nico Sep 19 '13 at 21:23
  • This is alot easier than working with regular expressions ! Thank you – pmagunia Sep 19 '13 at 21:28
  • @pmagunia That said, I suspect writing the data frame to file/connection and then piping through awk, it would be very easy to remove the row names, depending on the context. The `printed` representation of the table may not be what is wanted in all contexts. But glad you found the Answer helpful in this case. – Gavin Simpson Sep 19 '13 at 21:50
  • @GavinSimpson, what do you do in case of list of lists like : `C=list(listA = list(1:3, structure(1:9, .Dim = c(3L, 3L)), 4:9), listB = list(c("t1", "t2", "t3"), structure(c("p1", "p2"), .Dim = 2:1)))`, in this case `print(C,row.names=FALSE)` does not work ! – Haribo Sep 29 '18 at 11:17
4

Something like this should work:

apply(l, 1, function(x){cat(x); cat("\n")})
nico
  • 50,859
  • 17
  • 87
  • 112