sophie.h sophie.h - 3 months ago 22
C Question

name biiyuiiooi

I want to write a function.

I have tried so:

And for this function :

pl<-function(x,i, ...) {
ot(table(x[,i]))
}


But this function doesn't work and I don't know why this code is error.

Answer

Try this:

uebersicht <- function(daten, ask = TRUE) {
  par(ask = ask)
  plot.numeric <- function(x, nm, ...) boxplot(x, main = nm, ...)
  plot.factor <- function(x, nm, ...) barplot(table(x), main = nm, ...)
  mapply(function(x, nm) {
    if (is.factor(x) || is.numeric(x)) {
      plot(x, nm)
    } else {
      warning("unsupported type: ", sQuote(class(x)))
    }
  }, daten, names(daten))
}

The reason I put the definitions of plot.numeric and plot.factor within the function (instead of outside it as in your example) is that there are identically-named functions in the graphics namespace, and unless you intend the default behavior to change for every use even outside of uebersicht, this remapping should really only be done during the function call.

Sample usage:

set.seed(42)
par(mfrow = c(1,4))
weirdData <- data.frame(
  num_type = rexp(100),
  int_type = sample(100, size = 100, replace = TRUE),
  fac_type = sample(LETTERS[1:6], size = 100, replace = TRUE),
  char_type = sample(LETTERS[1:6], size = 100, replace = TRUE)
)
weirdData$char_type <- as.character(weirdData$char_type)
str(weirdData)
# 'data.frame': 100 obs. of  4 variables:
#  $ num_type : num  0.1983 0.6609 0.2835 0.0382 0.4732 ...
#  $ int_type : int  40 68 78 19 3 14 69 94 56 61 ...
#  $ fac_type : Factor w/ 6 levels "A","B","C","D",..: 4 1 5 1 4 4 4 5 1 2 ...
#  $ char_type: chr  "A" "E" "C" "E" ...
uebersicht(weirdData, ask = FALSE)
### ...snip... lots of output, should really be filtered or
### perhaps I should use 'invisible(mapply(...))'
# [1] "unsupported type: 'character'"

enter image description here

(Yes, I know the image has an empty fourth block ... I left it there because the naïve intention when calling the function was to plot four columns. *shrug*) The use of par(mfrow=c(1,4)) was merely for demonstration here on SO, as is the addition of the ask option to the function.

Comments