【问题标题】:How to range items in for loop in R?如何在R中的for循环中排列项目?
【发布时间】:2019-08-22 10:58:34
【问题描述】:

我想对 tibble 中未指定的行范围应用 for 循环。

我必须修改以下代码,在 tibble 中的特定行数上应用 for 循环:

for(times in unique(dat$wname)[1:111]){...} 

在这个 tibble 中从 1:111 的范围对应一个特定的文件,实际上“文件”列的值重复了 111 次。但是,在我的数据中,我不知道同一个文件重复了多少次。例如,我可以有一个重复 80 行的文件和另一个重复 85 行的文件。如何告诉循环仅在列文件中的行具有相同名称的范围内查找?

我有话要说:

for(times in unique(dat$wname)["for each row in the column File with the same name"]){...}

我该怎么做?

【问题讨论】:

标签: r for-loop


【解决方案1】:

如果你想在你的 dat 变量或 unique(dat$wname) 中使用列,你可以使用 nrow 或 ncol 来计算行数,然后执行以下操作:

rows= nrow(dat) # OR
rows=length(unique(dat$wname))

for(times in unique(dat$wname)[1:rows]){...}

但是一个可重复的例子会让事情更容易理解/回答

【讨论】:

  • @G.Frenandes,我想测试时间序列数据的平稳性,建立 ARIMA 模型,预测每 30 行时间序列,我该怎么做?我在同一个变量下有多个长格式的时间序列数据,一个时间序列有 30 年的数据,我想将 30 年的时间序列复制到 4 个,即 4*30=120 个观察值。非常感谢您的帮助!
猜你喜欢
  • 2019-11-25
  • 1970-01-01
  • 2019-11-19
  • 2023-03-27
  • 1970-01-01
  • 2021-02-27
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多