我想创建一个执行 for循环的函数以创建多个数据集。这些数据集应返回到单个数据集中,这将是我函数的输出。
我做了以下代码。当 for循环在函数外部时有效,但在循环在另一个函数内部时无效。我的函数存在的问题是,它只给我返回了第一个(i)数据集。
library(broom)
library(dplyr)
# My function
validation <- function(x, y) {
df <- NULL
for (i in 1:ncol(x)) {
coln <- colnames(x)[i]
covariate <- as.vector(x[,i])
models <- (tidy(glm(y ~ covariate, data = x, family = binomial)))
df <- (rbind(df, cbind(models, coln))) %>% filter( term != "(Intercept)")
return(df)
}
}
# Test function
validation(mtcars, mtcars$am)
term estimate std.error statistic p.value coln
covariate 0.3070282 0.1148416 2.673493 0.007506579 mpg
此功能应为我提供以下输出:
term estimate std.error statistic p.value coln
1 covariate 0.307028190 1.148416e-01 2.6734932353 0.007506579 mpg
2 covariate -0.691175096 2.536145e-01 -2.7252982408 0.006424343 cyl
3 covariate -0.014604292 5.167837e-03 -2.8259972293 0.004713367 disp
4 covariate -0.008117121 6.074337e-03 -1.3362973916 0.181452089 hp
5 covariate 5.577358500 2.062575e+00 2.7040753425 0.006849476 drat
6 covariate -4.023969940 1.436416e+00 -2.8013963535 0.005088198 wt
7 covariate -0.288189820 2.278968e-01 -1.2645629995 0.206028024 qsec
8 covariate 0.693147181 7.319250e-01 0.9470194188 0.343628884 vs
9 covariate 51.132135568 7.774641e+04 0.0006576784 0.999475249 am
10 covariate 21.006490452 3.876257e+03 0.0054192724 0.995676067 gear
11 covariate 0.073173343 2.254018e-01 0.3246350695 0.745457282 carb
答案 0 :(得分:1)
如果我们将return(df)
从内部循环更改为外部循环,它应该可以工作,因为内部循环内部的'df'返回只是刚刚更新的输出,即第一次运行的输出
validation <- function(x, y) {
df <- NULL
for (i in 1:ncol(x)) {
coln <- colnames(x)[i]
covariate <- as.vector(x[,i])
models <- (tidy(glm(y ~ covariate, data = x, family = binomial)))
df <- (rbind(df, cbind(models, coln))) %>% filter( term != "(Intercept)")
# to understand it better, create some print statement
print(sprintf("column index : %d", i))
print('-----------------')
print('df in each loop')
print(df)
print(sprintf("%dth loop ends", i))
}
df
}
-检查
validation(mtcars, mtcars$am)
# term estimate std.error statistic p.value coln
#1 covariate 0.307028190 1.148416e-01 2.6734932353 0.007506579 mpg
#2 covariate -0.691175096 2.536145e-01 -2.7252982408 0.006424343 cyl
#3 covariate -0.014604292 5.167837e-03 -2.8259972293 0.004713367 disp
#4 covariate -0.008117121 6.074337e-03 -1.3362973916 0.181452089 hp
#5 covariate 5.577358500 2.062575e+00 2.7040753425 0.006849476 drat
#6 covariate -4.023969940 1.436416e+00 -2.8013963535 0.005088198 wt
#7 covariate -0.288189820 2.278968e-01 -1.2645629995 0.206028024 qsec
#8 covariate 0.693147181 7.319250e-01 0.9470194188 0.343628884 vs
#9 covariate 51.132135568 7.774641e+04 0.0006576784 0.999475249 am
#10 covariate 21.006490452 3.876257e+03 0.0054192724 0.995676067 gear
#11 covariate 0.073173343 2.254018e-01 0.3246350695 0.745457282 carb