繁体   English   中英

R function 使用 for 循环返回 data.frame

[英]R function returning a data.frame using for loop

我想创建一个 function 执行for 循环来创建多个数据集。 这些数据集应该返回到单个数据集中,这将是我的 function 的 output。

我做了以下代码。 for 循环在 function 之外时它可以工作,但当循环在另一个 function 内部时它不起作用。 我的 function 的问题在于它只给了我第一个 (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

这个 function 应该给我以下 output:

  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”返回只是 output 刚刚更新,即第一次运行 output

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM