Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print list without line numbers in R

Tags:

r

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) 
like image 338
pmagunia Avatar asked Sep 19 '13 20:09

pmagunia


People also ask

How do I remove line numbers in R?

To remove line numbers from data. table object in R, we can set row. names to FALSE and print the data. table object.

How do I print a string in R?

To display ( or print) a text with R, use either the R-command cat() or print(). Note that in each case, the text is considered by R as a script, so it should be in quotes. Note there is subtle difference between the two commands so type on your prompt help(cat) and help(print) to see the difference.


2 Answers

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.

like image 107
Gavin Simpson Avatar answered Sep 20 '22 13:09

Gavin Simpson


Something like this should work:

apply(l, 1, function(x){cat(x); cat("\n")}) 
like image 31
nico Avatar answered Sep 23 '22 13:09

nico