Is there a list of variables in RStudio (or R), as in SPSS?

RStudio provides a nice function View(with uppercase V) to view the data, but with R it’s still unpleasant to get the orientation in a large data set. The most common are ...

  • names(df)
  • str(df)

If you come from SPSS, R seems like a downgrade in this regard. I wondered if there is a more user friendly option? I did not find the finished one, so I would like to share my decision with you.

+4
source share
1 answer

RStudio View, , data.frame, , SPSS. data.frame RStudio View.

# Better variables view
Varlist = function(sia) {
  # Init varlist output
  varlist = data.frame(row.names = names(sia))
  varlist[["comment"]] = NA
  varlist[["type"]] = NA
  varlist[["values"]] = NA
  varlist[["NAs"]] = NA
  # Fill with meta information
  for (var in names(sia)) {
    if (!is.null(comment(sia[[var]]))) {
        varlist[[var, "comment"]] = comment(sia[[var]])
    }
    varlist[[var, "NAs"]] = sum(is.na(sia[[var]]))
    if (is.factor(sia[[var]])) {
      varlist[[var, "type"]] = "factor"
      varlist[[var, "values"]] = paste(levels(sia[[var]]), collapse=", ")
    } else if (is.character(sia[[var]])) {
      varlist[[var, "type"]] = "character"
    } else if (is.logical(sia[[var]])) {
      varlist[[var, "type"]] = "logical"
      n = sum(!is.na(sia[[var]]))
      if (n > 0) {
        varlist[[var, "values"]] = paste(round(sum(sia[[var]], na.rm=T) / n * 100), "% TRUE", sep="")
      }
    } else if (is.numeric(sia[[var]])) {
      varlist[[var, "type"]] = typeof(sia[[var]])
      n = sum(!is.na(sia[[var]]))
      if (n > 0) {
        varlist[[var, "values"]] = paste(min(sia[[var]], na.rm=T), "...", max(sia[[var]], na.rm=T))
      }
    } else {
      varlist[[var, "type"]] = typeof(sia[[var]])
    }
  }
  View(varlist)
}

, (, Varlist.R) , , :

source("Varlist.R")
Varlist(df)

V, .

: data.frame , Varlist(df) .

. R print. R, View(varlist) print(varlist). , , Hmisc::describe() .

+4

Source: https://habr.com/ru/post/1657622/


All Articles