Home > Enterprise >  passing a list of dataframe into my own function
passing a list of dataframe into my own function

Time:10-01

i have a list

test2 <- list(a = tibble(qty = c(371.00000,125.00000,193.00000,8.00000,113.00000,247.00000,2.00000),
                         new_aur = c(83000,90000,110000, 114000, 117000, 119000, 120000)),
              b = tibble(qty = c(371.00000,125.00000,193.00000,8.00000,113.00000,247.00000,2.00000),
                         new_aur = c(80000,93000,120000, 113000, 117000, 119000, 120000))))

and a function

model_fitting_decay <- function(x) {
  set.seed(4123)
  x <- data.frame(x)
  trainIndex <- createDataPartition(x$qty, p = .8, 
                                    list = FALSE, 
                                    times = 1)
  
  df_data_train <- x[trainIndex,]
  
  trcontrol <- trainControl(method = "repeatedcv",
                            number = 10,
                            repeats = 3)
  
  model_train_decay <- lm(log(qty)~new_AUR, data=df_data_train)
  return(model_train_decay)
}

how can i passing my list as an argument in my function? i already tried using do.call but it returns error unused argument ( bla bla bla)

please help. thanks

CodePudding user response:

You can use lapply or purrr::map.

model_fitting_decay <- function(x) {
  set.seed(4123)
  x <- data.frame(x)
  trainIndex <- caret::createDataPartition(x$qty, p = .8, 
                                    list = FALSE, 
                                    times = 1)
  
  df_data_train <- x[trainIndex,]
  
  trcontrol <- caret::trainControl(method = "repeatedcv",
                            number = 10,
                            repeats = 3)
  
  model_train_decay <- lm(log(qty)~new_aur, data=df_data_train)
  return(model_train_decay)
}

lapply(test2, model_fitting_decay)
  • Related