0

I am using lapply to speed up data exploration. I am making my boxplots for the numeric variables all at once and I'd like to pass the variable name into the plot.

I have tried to define the name and pass it through.

library(ggplot2)
library(mass)
data(Boston)

num_vars = which(sapply(Boston, is.numeric))
ggBox <-function(index) {
  X <- index
  X_name <- names(index)
  ggplot(Boston,aes("X",X)) + geom_boxplot() + ggtitle(X_name)
}

lapply(Boston[names(num_vars)],FUN=ggBox)

I have tried different variants on that and it just passes X or a numeric value, without the name.

Khaynes
  • 1,976
  • 2
  • 15
  • 27
Sebastian
  • 957
  • 3
  • 15
  • 27
  • 2
    If `ggBox` takes as its first two arguments `x` and `title`, then you can do `Map(func, Boston[names(num_vars)], names(num_vars))`. If not, then you'll need to write a quick anonymous function, such as `Map(function(x,nm) ggBox(x, title=nm), Boston[names(num_vars)], names(num_vars))`. – r2evans Jan 08 '19 at 03:12

1 Answers1

0

lapply() can just pass object values to a function, and its names cannot be contained.

lapply(iris, FUN = names)

$Sepal.Length         $Sepal.Width         $Petal.Length
NULL                  NULL                 NULL

$Petal.Width          $Species
NULL                  NULL

You can see that every cell is NULL when I pass each column of a data frame to names(). If you want to use values and names at the same time, you need to design a bivariate function.

I take iris dataset for example:

ggBox <- function(x, name) {
  ggplot(iris, aes("X", x)) + geom_boxplot() + ggtitle(name)
}

df <- iris[1:4]
name <- names(iris)[1:4]

The following 3 methods are equivalent:

mapply(FUN = ggBox, df, name, SIMPLIFY = F)

Map(f = ggBox, df, name)

purrr::map2(df, name, ggBox)
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51