lowndrul
lowndrul

Reputation: 3815

R equivalent of Stata's 'summarize'?

In Stata, summarize prints a brief statistical summary of all variables in the current workspace. In R, summary(<myvariable>) does something similar for a particular <myvariable>.

Q: In R, how should I print a statistical summary of ALL relevant variables in my workspace?

I tried:

x <- runif(4)
y <- runif(4)
z <- runif(3)
w <- matrix(runif(4), nrow = 2)

sapply(ls(), function(i) {if (class(get(i)) == "numeric") summary(get(i))})

which gets close to what I want. But it still prints

$w
NULL
...

which is undesirable. Also, this code throws an error when there's a variable of type closure in my workspace...

I feel like I'm going off into the weeds here. There must be a simpler, straightforward way of more-or-less replicating Stata's summarize in R, right?

Upvotes: 2

Views: 4044

Answers (1)

Max Candocia
Max Candocia

Reputation: 4385

You can use methods to determine which variable types work with summary

summary.methods = methods(summary)
check.method <- function(x){ 
  any(grepl(paste0('^summary\\.',class(x)[1],'$'),summary.methods))
}
lapply(ls(), function(z,envir = .GlobalEnv) {
 obj = get(z)
 if (class(obj) %in% c('list','data.frame')
    Recall(names(obj),as.environment(obj))
 else if (check.method(obj))
  print(summary(obj))
 else
  print(paste0("No summary for: ",z))
})

You may want to change this depending on how much data you have, but it should work.

Added some recursion for list/data frames.

If you want to get it to work with lists and individual data frame columns, I would check for those classes and use as.environment to get variables from the list/frame. I can show you a more explicit way of doing this later if you like.

Upvotes: 1

Related Questions