0

I want to do graphical outputs such as boxplots or graphs using a function. So that I can plot several dataframes, changing only the column name each time.

For example :

boxplot_func = function(column){
  boxplot(dataframe1$column, dataframe2$column)}

boxplot_func(mean)
boxplot_func(max)
etc.

But R doesn't seem to compute mean or max in the function. Do you know a way to do it ?

Anthony
  • 39
  • 6

1 Answers1

3

One option would be to pass the column as a character string and use [[ to access the column in your function:

A simple example using mtcars:

boxplot_func = function(column) {
  boxplot(mtcars[[column]], mtcars[[column]])
}

boxplot_func("mpg")

stefan
  • 90,330
  • 6
  • 25
  • 51