Getting count from a loop

I have a loop in my code that pulls summary statistics from each logistic regression I perform on each dataset. I would like it to also show the count of each level for each variable if possible. I am pretty new to loops though, and am not quite sure how to ask R how to do it.

Here is an example of the code I am currently using. The data I am using is different but the rest of the analysis is nearly the same.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>library(tidyverse)
install.packages("AER")
library("AER")
data(Affairs, package="AER")
Affairs$ynaffair[Affairs$affairs > 0] <- 1
Affairs$ynaffair[Affairs$affairs == 0] <- 0
Affairs <- Affairs %>%
mutate_at(c("affairs", "religiousness", "occupation", "rating", "ynaffair"), as.factor)
frmlas <- list(ynaffair~gender,
ynaffair~children,
ynaffair~religiousness,
ynaffair~occupation,
ynaffair~rating)
output <- list()
output_df_list <- list()
for(i in 1:length(frmlas)){
output[[i]] <- glm(frmlas[[i]], data = Affairs, family=binomial)
names(output)[i] <- capture.output(frmlas[[i]])
output_df_list[[i]] <- data.frame("Model"=capture.output(frmlas[[i]]),
"Covariate"=names(output[[i]]$coefficients),
"Beta_Estimate"=output[[i]]$coefficients,
"P_val"=summary(output[[i]])$coefficients[,4],
"CI_95_LL"=confint(output[[i]])[,1],
"CI_95_UL"=confint.default(output[[i]])[,2])
rownames(output_df_list[[i]]) <- NULL
}
output_df_full_t1 <- do.call("rbind", output_df_list)
output_df_full_t1 <- output_df_full_t1 %>%
mutate(OR = exp(Beta_Estimate)) %>%
mutate(CI_95_LL = exp(CI_95_LL),
CI_95_UL = exp(CI_95_UL)) %>%
select(Model, Covariate, OR, CI_95_LL, CI_95_UL, P_val) %>%
filter(!(Covariate %in% '(Intercept)')) %>%
mutate_if(is.numeric, round, digits = 3) %>%
unite(CI, c(CI_95_LL, CI_95_UL), sep = ", ", remove = TRUE)
head(output_df_full_t1)
</code>
<code>library(tidyverse) install.packages("AER") library("AER") data(Affairs, package="AER") Affairs$ynaffair[Affairs$affairs > 0] <- 1 Affairs$ynaffair[Affairs$affairs == 0] <- 0 Affairs <- Affairs %>% mutate_at(c("affairs", "religiousness", "occupation", "rating", "ynaffair"), as.factor) frmlas <- list(ynaffair~gender, ynaffair~children, ynaffair~religiousness, ynaffair~occupation, ynaffair~rating) output <- list() output_df_list <- list() for(i in 1:length(frmlas)){ output[[i]] <- glm(frmlas[[i]], data = Affairs, family=binomial) names(output)[i] <- capture.output(frmlas[[i]]) output_df_list[[i]] <- data.frame("Model"=capture.output(frmlas[[i]]), "Covariate"=names(output[[i]]$coefficients), "Beta_Estimate"=output[[i]]$coefficients, "P_val"=summary(output[[i]])$coefficients[,4], "CI_95_LL"=confint(output[[i]])[,1], "CI_95_UL"=confint.default(output[[i]])[,2]) rownames(output_df_list[[i]]) <- NULL } output_df_full_t1 <- do.call("rbind", output_df_list) output_df_full_t1 <- output_df_full_t1 %>% mutate(OR = exp(Beta_Estimate)) %>% mutate(CI_95_LL = exp(CI_95_LL), CI_95_UL = exp(CI_95_UL)) %>% select(Model, Covariate, OR, CI_95_LL, CI_95_UL, P_val) %>% filter(!(Covariate %in% '(Intercept)')) %>% mutate_if(is.numeric, round, digits = 3) %>% unite(CI, c(CI_95_LL, CI_95_UL), sep = ", ", remove = TRUE) head(output_df_full_t1) </code>
library(tidyverse)
install.packages("AER")
library("AER")
data(Affairs, package="AER")
Affairs$ynaffair[Affairs$affairs >  0] <- 1
Affairs$ynaffair[Affairs$affairs == 0] <- 0

Affairs <- Affairs %>% 
  mutate_at(c("affairs", "religiousness", "occupation", "rating", "ynaffair"), as.factor) 

frmlas <- list(ynaffair~gender,
               ynaffair~children,
               ynaffair~religiousness,
               ynaffair~occupation,
               ynaffair~rating)
output <- list() 
output_df_list <- list() 

for(i in 1:length(frmlas)){
  output[[i]] <- glm(frmlas[[i]], data = Affairs, family=binomial)
  names(output)[i] <- capture.output(frmlas[[i]]) 
  output_df_list[[i]] <- data.frame("Model"=capture.output(frmlas[[i]]),
                                    "Covariate"=names(output[[i]]$coefficients),
                                    "Beta_Estimate"=output[[i]]$coefficients,
                                    "P_val"=summary(output[[i]])$coefficients[,4],
                                    "CI_95_LL"=confint(output[[i]])[,1],
                                    "CI_95_UL"=confint.default(output[[i]])[,2])
    rownames(output_df_list[[i]]) <- NULL             
}  

output_df_full_t1 <- do.call("rbind", output_df_list)

output_df_full_t1 <- output_df_full_t1 %>% 
  mutate(OR = exp(Beta_Estimate)) %>% 
  mutate(CI_95_LL = exp(CI_95_LL),
         CI_95_UL = exp(CI_95_UL))  %>% 
  select(Model, Covariate, OR, CI_95_LL, CI_95_UL, P_val) %>% 
  filter(!(Covariate %in% '(Intercept)'))  %>% 
  mutate_if(is.numeric, round, digits = 3)   %>% 
  unite(CI, c(CI_95_LL, CI_95_UL), sep = ", ", remove = TRUE) 

head(output_df_full_t1)

Output snip

This is where I found this piece of code, and then modified it slightly:
https://bookdown.org/kdonovan125/ibis_data_analysis_r4/intro-7.html

The expected result would be a new column with the counts of each level in the output. Or if that isn’t possible, and you have an alternative suggestion, I would be grateful for the advice. My mentor/boss just doesn’t want me copy/ pasting the counts for each one. The end goal is to output the result to an excel file.

Desired final output

I need to do this many times, so figuring this out would be very time-saving for me.

Thanks!

New contributor

Newtostats_24 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật