[R] purrr::pmap does not work

G.Maubach at weinwolf.de G.Maubach at weinwolf.de
Wed Jun 7 16:34:43 CEST 2017


Hi All,

I try to do a scatterplot for a bunch of variables. I plot a dependent 
variable against a bunch of independent variables:

-- cut --
graphics::plot(
  v01_r01 ~ v08_01_up11,
  data = dataset,
  xlab = "Dependent",
  ylab = "Independent #1"
)

-- cut --

It is tedious to repeat the statement for all independent variables. Found 
an alternative, i.e. :

-- cut --

mu <- list(5, 10, -3)
sigma <- list(1, 5, 10)
n <- list(1, 3, 5)
fargs <- list(mean = mu, sd = sigma, n = n)
fargs %>%
  purrr::pmap(rnorm) %>%
  str()

-- cut --

I tried to use this for may scatterplot task:

-- cut --

var_battery$v08 <- paste0("v08_", formatC(1:8, width = 2, format = "d", 
flag = "0"))
v08_var_labs <- paste0("Label_", 1:8)

dataset <- as.data.frame(
  matrix(
    data = sample(
      x = 1:11,
      size = 90,
      replace = TRUE),
    nrow = 10,
    ncol = 9))
names(dataset) <- c("v01_r01", var_battery$v08)

independent <- as.list(dataset$v01_r01)
dependent <- as.list(dataset[var_battery$v08])

fargs <- list(
  x = independent,
  y = dependent,
  ylab = v08_var_labs)

fargs %>% 
  purrr::pmap(
    function(d = dataset, xvalue = x, yvalue = y,
             xlab = "Label for x variable",
             ylab = ylab) {
      graphics::plot(
        xvalue ~ yvalue,
        data = d,
        xlab = xlab,
        ylab = ylab)
    }
  )

-- cut --

The last statement comes back with

Error: Element 2 has length 8, not 1 or 10.

How can I get it up n running? Do you suggest a better solution for the 
task described?

Kind regards

Georg



More information about the R-help mailing list