首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将输出从循环导出到数据帧?

如何将输出从循环导出到数据帧?
EN

Stack Overflow用户
提问于 2022-08-24 01:08:59
回答 1查看 68关注 0票数 -1

我使用自定义的线性回归函数reg()生成了一些预测值,并使用for-循环对多个变量进行迭代。它们保存在forage.pred中,这是一个5000多个值的向量。

我现在正试图将它转换成一个数据文件,但我不知道如何。

我在互联网上找到的几个循环教程使用了基于单个向量的一个向量和索引的虚拟示例,因此它们不适用于我的情况。

代码语言:javascript
复制
forage
# A tibble: 5,421 × 24
#    year standid trt    plot sedge legume woody forbs
#   <dbl>   <dbl> <chr> <dbl> <dbl>  <dbl> <dbl> <dbl>
# 1  1986       2 CCSP      1    30   14.4     0   30 
# 2  1986       7 CCSP      1     0    0      20    0 
# 3  1986      12 CCSP      1    18    0       0  158.
# 4  1987       2 CCSP      1     2    0       0   32 
# 5  1987       7 CCSP      1     0   40       0   80 
# 6  1987      12 CCSP      1     0    2       0   52 
# 7  1988       2 CCSP      1     0    2     424    2 
# 8  1988       7 CCSP      1   104    0       0    0 
# 9  1988      12 CCSP      1     4    4      64    2 
# 10  1989       2 CCSP      1     0    0       0    2 
# … with 5,411 more rows, and 16 more variables:
#   panicum <dbl>, grass <dbl>, litter <dbl>,
#   burned <dbl>, nonwoody <dbl>, pp_grow <dbl>,
#   pp_watyear <dbl>, pp_spring <dbl>, pp_summer <dbl>,
#   pp_winter <dbl>, pp_grow.s50 <dbl>,
#   pp_grow.s30 <dbl>, pp_grow.s10 <dbl>,
#   pp_grow.a10 <dbl>, pp_grow.a30 <dbl>, …
# ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names

yvar <- forage[, 5:10]
new.pp <- forage[, 19:24]

# Fit linear model and create predicted values
reg <- function(i, j) {
  lm.forage <- lm(i ~ year + pp_grow,
                 data = forage)
  forage.pred <- (predict(lm.forage,
                          newdata = list(pp_grow = j)))
  }

# Loop this over multiple variables
for (i in yvar) {
  for (j in new.pp) {
    reg(i, j)
  }
}
EN

回答 1

Stack Overflow用户

发布于 2022-08-24 12:42:15

我认为所有关于循环回归的初步文本都混淆了什么是简单的问题和一个简单的答案。问题,你如何取一个数值向量,并转换成一个数据.一般情况下会很简单

代码语言:javascript
复制
(my_vector <- 1:5)
(my_dataframe <- data.frame(myv = my_vector))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73466418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档