Printing a data frame R with column names in multiple rows

I have an R data frame with long column names. Therefore, when I print a data frame, it is too wide. Is there an easy way to print a data frame on the screen with code names appearing in several lines?

I know that I can shorten names, but I would not want to.

+7
source share
3 answers

When Oscar’s answer was accepted, I thought it was actually the answer. Unfortunately, all that was was copying the code from format.daa.frame and saying "do some vague magic here." Here is some code that actually does something, although I thought it was too rude to publish at that time. It still prints the row and column headings of the matrix. I do not know how to suppress it. May need a hacked printing method for matrices?

 dfrm <- data.frame(reallly_long.nameeeeeeeeeeeeeeeeeeeeeeee=letters[1:5], secondreallly_long.nameeeeeeeeeeeeeeeeeeeeeeee=letters[1:5], short=2) pdfrm <- function(dfrm) { # first print the names broken into sections print( unname(t(sapply( 1:(max(nchar(names(dfrm))) %/% 12), # first construct break points to be passed to `substr` function(rr) sapply(names(dfrm), substr, 1+(rr-1)*10, 9+(rr-1)*10) ) )) ,quote=FALSE, # then print with sufficient gap print.gap=8) # Now print a headerless data.frame with wider spacing print( setNames(dfrm, rep(" ", length(dfrm))), print.gap = 15 )} pdfrm(dfrm) #------------------------------ [,1] [,2] [,3] [1,] reallly_l secondrea short [2,] ng.nameee lly_long. [3,] eeeeeeeee ameeeeeee 1 aa 2 2 bb 2 3 cc 2 4 dd 2 5 ee 2 
+2
source

You can create a custom function format.data.frame with changes in behavior when R finds "long" names:

 long <- nchar(cn, "bytes") > 256L cn[long] <- paste(substr(cn[long], 1L, 250L), "...") names(rval) <- cn 

And since dataframs print as matrices after all this preprocessing, it is hoped that print.default (in fact, common print.default ) handles escape characters like cat does (print as is to get a new line when printing \n ) .

Edit Actually, print.default hides non-printable characters (online cat help page)

Character strings are printed "as is" (unlike print.default, which escapes non-printable characters and backslash [...])

So, you should start preparing your own method (also, check the parameters when converting R objects to $$ \ LaTeX $$ tables, maybe there is an option).

+2
source

REMOVING THE ROW AND COLUMNS MATRIX

I somehow edited the code 42- above to solve the problem of deleting row and column indexes of a matrix by creating a modification of the print function. Here is the code:

 pdfrm <- function(dfrm) { # first print the names broken into sections mprint <- function(Mn){ rownames(Mn) <- rep("",nrow(Mn)) colnames(Mn) <- rep("",ncol(M)) print(as.table(Mn),quote=FALSE,print.gap=8) } mprint(unname( t(sapply( 1:(max(nchar(names(dfrm))) %/% 12), # first construct break points to be passed to `substr` function(rr) sapply(names(dfrm), substr, 1+(rr-1)*10, 9+(rr-1)*10) ) ))) # Now print a headerless data.frame with wider spacing print( setNames(dfrm, rep(" ", length(dfrm))), print.gap = 15 ) } pdfrm(dfrm) 

and it produces the following:

  reallly_l secondrea short ng.nameee lly_long. eeeeeeeee ameeeeeee 1 aa 2 2 bb 2 3 cc 2 4 dd 2 5 ee 2 

The above code can be modified so that it can take care of an arbitrary and adaptive distance.

+1
source

All Articles