Truncating Column Names When Printing a Data Frame

R abbreviate () is useful for truncating, among other things, the column names of a dataframe with a given length, with good checks to ensure uniqueness, etc .:

abbreviate(names(dframe), minlength=2)

      

You can, of course, use this function to truncate the column names in place and then print the modified dataframe

>>names(dframe) <- abbreviate(names(dframe), minlength=2)
>>dframe

      

But I would like to print a dataframe with shortened column names without changing the dataframe in the process. Hopefully this can be done with a simple format in the print () call, although my search through the help pages for print and formatting methods such as print.data.frame did not reveal any obvious solution (the options available are more suitable for formatting values column, not their names).

So, does print () or format () have any parameters that cause shorthand () for column names? If not, is there a way to apply abbreviate () to the column names of the dataframe before passing it to print (), without modifying the transmitted dataframe?

The more I think about it, the more I think that the only way would be to pass print () a copy of the dataframe with the column names already abbreviated. But this is not a solution for me, because I do not want to constantly update this copy when I update the original during an interactive session. The original column names should remain unchanged because I am using (colnames (dframe) == "name_of_column") to interact with the data.

My ultimate goal is to remotely work on the small screen of my mobile device while running applications like Server Auditor. If the column names are shortened to 2-3 characters, I can still recognize them, but much more data can be added to the screen. Perhaps there are even R-bags that are better suited for condensed printing?

+3


source to share


2 answers


You can define your own printing method

print.myDF <- function(x, abbr = TRUE, minlength = 2, ...) {
    if (abbr) {
        names(x) <- abbreviate(names(x), minlength = minlength) 
    }
    print.data.frame(x, ...)
}

      



Then add the class myDF

to the data and print

class(iris) <- c("myDF", class(iris))
head(iris, 3)
#   S.L S.W P.L P.W     Sp
# 1 5.1 3.5 1.4 0.2 setosa
# 2 4.9 3.0 1.4 0.2 setosa
# 3 4.7 3.2 1.3 0.2 setosa
print(head(iris, 3), abbr = FALSE)
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
print(head(iris, 3), minlength = 5)
#   Spl.L Spl.W Ptl.L Ptl.W  Specs
# 1   5.1   3.5   1.4   0.2 setosa
# 2   4.9   3.0   1.4   0.2 setosa
# 3   4.7   3.2   1.3   0.2 setosa

      

+3


source


Just rewrite print.data.frame

:

 print.data.frame <-
               function(x) setNames( print(x), 
                                abbreviate(names(dframe), minlength=2) )

      



(You might need an additional printfull.data.frame to which you copy the print.data.frame first.)

+1


source







All Articles