There is probably a more elegant way for me to extract the coefficient and SE, but this is what I did:
data <- mtcars[, c("mpg", "cyl", "disp", "hp", "drat", "wt")]
col10 <- names(data)[-1]
lm.test <- vector("list", length(col10))
for(i in seq_along(col10)){
lm.test[[i]] <- lm(reformulate(col10[i], "mpg"), data = data)
}
lm.test_coef <- lapply(lm.test, summary)
# extract the coefficient (X2)
lm.test_coef_df <- data.frame(matrix(unlist(lm.test_coef), ncol = max(lengths(lm.test_coef)), byrow = TRUE))
# turn list into dataframe
lm.test_coef_df <- as.data.frame(t(simplify2array(lm.test_coef)))
# extract coefficients column
lm.test_coef_df_i <- dplyr::select(lm.test_coef_df, terms, coefficients)
# flatten list
lm.test_coef_df_i <- apply(lm.test_coef_df_i,2,as.character)
View(lm.test_coef_df_i)
lm.test_coef_df_i <- as.data.frame(lm.test_coef_df_i)
# remove strings
lm.test_coef_df_i$coefficients <- stringr::str_replace(lm.test_coef_df_i$coefficients, '\\c', '')
lm.test_coef_df_i$coefficients <- stringr::str_replace(lm.test_coef_df_i$coefficients, '\\(', '')
lm.test_coef_df_i$coefficients <- stringr::str_replace(lm.test_coef_df_i$coefficients, '\\)', '')
lm.test_coef_df_i <- cSplit(lm.test_coef_df_i, "coefficients", sep=",")
lm.coef_sd <- dplyr::select(lm.test_coef_df_i, coefficients_2, coefficients_4)
View(lm.coef_sd)