R结合多个`data.frames`(`lapply`,`mapply`)
示例
在本练习中,我们将生成四个Bootstrap线性回归模型,并将这些模型的摘要合并为一个数据框。
library(broom) #* Create the bootstrap data sets BootData <- lapply(1:4, function(i) mtcars[sample(1:nrow(mtcars), size = nrow(mtcars), replace = TRUE), ]) #* Fit the models Models <- lapply(BootData, function(BD) lm(mpg ~ qsec + wt + factor(am), data = BD)) #* Tidy the output into a data.frame Tidied <- lapply(Models, tidy) #* Give each element in the Tidied list a name Tidied <- setNames(Tidied, paste0("Boot", seq_along(Tidied)))
此时,我们可以采用两种方法将名称插入到data.frame中。
#* Insert the element name into the summary with `lapply` #* Requires passing the names attribute to `lapply` and referencing `Tidied` within #* the applied function. Described_lapply <- lapply(names(Tidied), function(nm) cbind(nm, Tidied[[nm]])) Combined_lapply <- do.call("rbind", Described_lapply) #* Insert the element name into the summary with `mapply` #* Allows us to pass the names and the elements as separate arguments. Described_mapply <- mapply( function(nm, dframe) cbind(nm, dframe), names(Tidied), Tidied, SIMPLIFY = FALSE) Combined_mapply <- do.call("rbind", Described_mapply)
如果您是magrittr样式管道的爱好者,则可以在一个链中完成整个任务(尽管如果需要任何中间对象(例如模型对象本身),这样做可能并不明智):
library(magrittr) library(broom) Combined <- lapply(1:4, function(i) mtcars[sample(1:nrow(mtcars), size = nrow(mtcars), replace = TRUE), ]) %>% lapply(function(BD) lm( mpg ~ qsec + wt + factor(am), data = BD)) %>% lapply(tidy) %>% setNames(paste0("Boot", seq_along(.))) %>% mapply(function(nm, dframe) cbind(nm, dframe), nm = names(.), dframe = ., SIMPLIFY = FALSE) %>% do.call("rbind", .)