首页 > 解决方案 > 如何在 R 中使用更改的数据名称进行循环

问题描述

我想像这样在 R 中创建一个循环函数:

threshold_month = 3
dta_3 <- dta_fin %>%
  filter(month < threshold_month)

threshold_month = 6
dta_6 <- dta_fin %>%
  filter(month < threshold_month)

threshold_month = 9
dta_9 <- dta_fin %>%
  filter(month < threshold_month)

如何使用阈值 3、6 和 9.. 制作循环功能?

标签: rloops

解决方案


你可以试试这个:

for (threshold_month in c(3, 6, 9)) {
    assign(paste0("dta_", threshold_month), filter(dta_fin, month < threshold_month))
}

推荐阅读