Hello, is it possible to show only the header (that is: `'data.frame': x obs. of y variables:` part) of the str function? Thank you -- Best regards, Luigi
Hello, Not perfect but works for data.frames: header_str <- function(x){ capture.output(str(x))[[1]] } header_str(iris) header_str(AirPassengers) header_str(1:10) Hope this helps, Rui Barradas ?s 12:02 de 02/09/21, Luigi Marongiu escreveu:> Hello, is it possible to show only the header (that is: `'data.frame': > x obs. of y variables:` part) of the str function? > Thank you >
Luigi, If you are sure you are looking at something like a data.frame, and all you want o know is how many rows and how many columns are in it, then str() is perhaps too detailed a tool. The functions nrow() and ncol() tell you what you want and you can get both together with dim(). You can, of course, print out whatever message you want using the numbers supplied by throwing together some function like this: sstr <- function(x) { cat(nrow(x), "obs. of ", ncol(x), " variables\n") } Calling that instead of str may meet your needs. Of course, unlike str, it will not work on arbitrary data structures. Note the output of str()goes straight to the screen, similar to what cat does. Capturing the output to say chop out just the first line is not therefore a simple option. -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Luigi Marongiu Sent: Thursday, September 2, 2021 7:02 AM To: r-help <r-help at r-project.org> Subject: [R] Show only header of str() function Hello, is it possible to show only the header (that is: `'data.frame': x obs. of y variables:` part) of the str function? Thank you -- Best regards, Luigi ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On Thu, 02 Sep 2021, Luigi Marongiu writes:> Hello, is it possible to show only the header (that is: `'data.frame': > x obs. of y variables:` part) of the str function? > Thank youPerhaps one more solution. You could limit the number of list components to be printed, though it will leave a "truncated" message. str(iris, list.len = 0) ## 'data.frame': 150 obs. of 5 variables: ## [list output truncated] Since 'str' is a generic function, you could also define a new 'str' method. Perhaps something among those lines: str.data.frame.oneline <- function (object, ...) { cat("'data.frame':\t", nrow(object), " obs. of ", (p <- length(object)), " variable", if (p != 1) "s", "\n", sep = "") invisible(NULL) } (which is essentially taken from 'str.data.frame'). Then: class(iris) <- c("data.frame.oneline", class(iris)) str(iris) ## 'data.frame': 150 obs. of 5 variables str(list(a = 1, list(b = 2, c = iris))) ## List of 2 ## $ a: num 1 ## $ :List of 2 ## ..$ b: num 2 ## ..$ c:'data.frame': 150 obs. of 5 variables -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net