这应该可行:
set.seed(123) ## for sake of reproducibility
n <- 10
dat <- data.frame(balance=factor(paste("DM", 1:n)),
credit_history=sample(c("repaid", "critical"), 10, replace = TRUE),
purpose=sample(c("yes", "no"), 10, replace = TRUE),
employment_rate=sample(c("0-1 yrs", "1-4 yrs", ">4 yrs"), 10, replace = TRUE),
personal_status=sample(c("married", "single"), 10, replace=TRUE),
other_debtors=sample(c("guarantor", "none"), 10, replace= TRUE),
default=sample(c("yes", "no"), 10, replace = TRUE))
dt1 <- dat[ , c(1:6)]
dt2 <- dat[ , "default"]
mapply(function(x, y, z) {table(balance=x, default=y)}, dt1, MoreArgs=list(dt2))
#> $balance
#> default
#> balance no yes
#> DM 1 0 1
#> DM 10 1 0
#> DM 2 0 1
#> DM 3 0 1
#> DM 4 1 0
#> DM 5 1 0
#> DM 6 0 1
#> DM 7 1 0
#> DM 8 0 1
#> DM 9 0 1
#>
#> $credit_history
#> default
#> balance no yes
#> critical 2 2
#> repaid 2 4
#>
#> $purpose
#> default
#> balance no yes
#> no 2 3
#> yes 2 3
#>
#> $employment_rate
#> default
#> balance no yes
#> >4 yrs 2 0
#> 0-1 yrs 1 4
#> 1-4 yrs 1 2
#>
#> $personal_status
#> default
#> balance no yes
#> married 3 3
#> single 1 3
#>
#> $other_debtors
#> default
#> balance no yes
#> guarantor 4 3
#> none 0 3
mapply(function(x, y, z) {table(x, y, dnn=c(z,'default'))}, dt1,z=names(dt1), MoreArgs=list(dt2))
#> $balance
#> default
#> balance no yes
#> DM 1 0 1
#> DM 10 1 0
#> DM 2 0 1
#> DM 3 0 1
#> DM 4 1 0
#> DM 5 1 0
#> DM 6 0 1
#> DM 7 1 0
#> DM 8 0 1
#> DM 9 0 1
#>
#> $credit_history
#> default
#> credit_history no yes
#> critical 2 2
#> repaid 2 4
#>
#> $purpose
#> default
#> purpose no yes
#> no 2 3
#> yes 2 3
#>
#> $employment_rate
#> default
#> employment_rate no yes
#> >4 yrs 2 0
#> 0-1 yrs 1 4
#> 1-4 yrs 1 2
#>
#> $personal_status
#> default
#> personal_status no yes
#> married 3 3
#> single 1 3
#>
#> $other_debtors
#> default
#> other_debtors no yes
#> guarantor 4 3
#> none 0 3
由reprex package (v2.0.1) 于 2021-12-06 创建