0
votes

To improve my understanding of the R libraries purrr and dplyr I am working through an example described by Hadley Wickham using different models and data set splits. However, I run into into a coercion error when using multiple arguments e.g. using map2() and pmap():

(list) object cannot be coerced to type 'integer'

I have simplified my software to try and pin down where I am going wrong by using the following:

    library(dplyr)
    library(purrr)
    library(randomForest)

    # generate a list of data frames ( using the iris data set )

    data_list <- lapply(1:10, FUN = function(x) iris)

    # generate a list of parameters:

    param_list <- as.list(seq.int(100,1000, by = 100))

    # - following works

    models <- data_list %>% map(~randomForest(Species ~., data = .))

   # - following works

    models <- seq_along(param_list) %>% 
              map(~randomForest(Species ~., data = data_list[[.]],
                                           ntree = param_list[[.]]))

    # - following has error: Error in randomForest.default(m, y, ...) : 
    #     (list) object cannot be coerced to type 'integer'

     models <- map2(data_list, param_list, 
                 ~randomForest(Species ~., data = ., ntree = .))

I have been struggling with this problem for a few days therefore I am probably confused with lists, data frames etc. therefore grateful for any help.

1

1 Answers

2
votes

You should be ok with :

models <- map2(data_list, param_list, 
               ~randomForest(Species ~., data = .x, ntree = .y))

In map2, implicit args to anonymous functions on the RHS of a formula are .x and .y.