r - R函数使用for循环返回data.frame
问题描述
我想创建一个执行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
解决方案
如果我们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
推荐阅读
- python - 如何在python中使用前缀字段更改AFINN字段
- algorithm - 使用反向替换计算算法的复杂度
- blazor - 将 EditFrom 拆分为多个组件
- node.js - 如何在 AWS sdk nodejs 中更改 aws 配置文件
- google-cloud-platform - 在 GCP cloudbuild.yaml 中指定与“名称”不同的“执行函数”
- javascript - JavaScript add 和 removeEventListeners 带参数的回调
- gitlab - 未能加载代码气候报告,但报告存在
- python - 开始使用 mypy / 忽略外部库
- python - Python Selenium - 如何单击非按钮元素
- c# - C# 只允许一个类调用不同类的属性设置器(或者让类知道它是否存储在另一个类的数组中)