Run lapply with lmer function on list of list
I am struggling with launching the lmer
function on a data set comprising a list of lists. I have tried converting the list as a data frame too, but it returns an error
lapply(list, function(x) lapply(x, function(x) as.data.frame(x)))
lapply(list, function(x) lapply(x, function(x)
lmer(x[,4:7] ~ var + (1| id), x)))
Error in model.frame.default(data = x, drop.unused.levels = TRUE, formula = x[, : not valid type (list) for the variable 'x'
Can anyone suggest something for running lmer
through lapply
?
reprex
library(tidyverse)
library(readxl)
require(writexl)
write_xlsx(mtcars, 'PQ_ele_com.xlsx')
dir = c('com', 'set', 'rit')
init = c('PQ', 'MG')
inpst = c('_ele_', '_mel_', '_col_')
for (i in dir) {
for (j in init) {
for (k in inpst){
write_xlsx(mtcars, paste0(j, k, i, '.xlsx'))
}
}
}
files = list.files(recursive= FALSE, full.names= FALSE)
init = c('PQ', 'MG')
dir = c('com', 'set', 'rit')
list3 = NULL
for (j in init){
for (k in dir){
df=c()
for (i in files){
if(str_detect(i, pattern = j) & str_detect(i, pattern = k)) {
df=rbind(df,read_excel(i))}
}
list3[[j]][[k]] = df
}
}
Let's suppose I would like to fit lmer
model through each sublists. For example:
lapply(list3, function(x) lapply(x,
function(x) lmer(x[,3:7] ~ vs + (1| cyl), x)))
It returns the error reported. Do you think it is possible to coerce it somehow?
Comments
Post a Comment