基于向量长度的向量递归函数

Recursive function over a vector based on the length of it

我有几个月经间隔 6 个月。首字母“in_talls_temp_6”和结尾“f_talls_temp_6”。

`in_talls_temp_6 <- seq.Date(from=i_preImp_preref, to=f_postImp, by="6 months")
f_talls_temp_6 <- in_talls_temp_6 + months(6) - days(1)

我有这样的数据:

name <- paste0("time_point", seq(1:13))
a <- round(runif(length(name), 200, 500), 0)

data <- data.frame(name, a)

           name   a
1   time_point1 361
2   time_point2 444
3   time_point3 221
4   time_point4 434
5   time_point5 400
6   time_point6 438
7   time_point7 411
8   time_point8 367
9   time_point9 409
10 time_point10 337
11 time_point11 481
12 time_point12 201
13 time_point13 417

而且我想为每个“time_point”分配 x 期的初始日期和结束日期。

到目前为止,我都是用愚蠢的方式来做的:

data %>% 
  mutate( i.date.time.point = case_when (name == "time_point1" ~ in_talls_temp_6[1],
                                       name == "time_point2" ~ in_talls_temp_6[2],
                                       name == "time_point3" ~ in_talls_temp_6[3],
                                       name == "time_point4" ~ in_talls_temp_6[4],
                                       name == "time_point5" ~ in_talls_temp_6[5],
                                       name == "time_point6" ~ in_talls_temp_6[6],
                                       name == "time_point7" ~ in_talls_temp_6[7],
                                       name == "time_point8" ~ in_talls_temp_6[8],
                                       name == "time_point9" ~ in_talls_temp_6[9],
                                       name == "time_point10" ~ in_talls_temp_6[10],
                                       name == "time_point11" ~ in_talls_temp_6[11],
                                       name == "time_point12" ~ in_talls_temp_6[12],
                                       name == "time_point13" ~ in_talls_temp_6[13]) ) %>%
  mutate( f.date.time.point = case_when (name == "time_point1" ~ f_talls_temp_6[1],
                                         name == "time_point2" ~ f_talls_temp_6[2],
                                         name == "time_point3" ~ f_talls_temp_6[3],
                                         name == "time_point4" ~ f_talls_temp_6[4],
                                         name == "time_point5" ~ f_talls_temp_6[5],
                                         name == "time_point6" ~ f_talls_temp_6[6],
                                         name == "time_point7" ~ f_talls_temp_6[7],
                                         name == "time_point8" ~ f_talls_temp_6[8],
                                         name == "time_point9" ~ f_talls_temp_6[9],
                                         name == "time_point10" ~ f_talls_temp_6[10],
                                         name == "time_point11" ~ f_talls_temp_6[11],
                                         name == "time_point12" ~ f_talls_temp_6[12],
                                         name == "time_point13" ~ f_talls_temp_6[13])
          )

得到这个:

               name   a i.date.time.point f.date.time.point
1   time_point1 361        2014-07-01        2014-12-31
2   time_point2 444        2015-01-01        2015-06-30
3   time_point3 221        2015-07-01        2015-12-31
4   time_point4 434        2016-01-01        2016-06-30
5   time_point5 400        2016-07-01        2016-12-31
6   time_point6 438        2017-01-01        2017-06-30
7   time_point7 411        2017-07-01        2017-12-31
8   time_point8 367        2018-01-01        2018-06-30
9   time_point9 409        2018-07-01        2018-12-31
10 time_point10 337        2019-01-01        2019-06-30
11 time_point11 481        2019-07-01        2019-12-31
12 time_point12 201        2020-01-01        2020-06-30
13 time_point13 417        2020-07-01        2020-12-31
    

我觉得有更好的方法,但我做不到。我被困在这里是因为我想扩大项目规模,现在我想做同样的事情:

in_talls_temp_3 <- seq.Date(from=i_preImp_preref, to=f_postImp, by="3 months")
f_talls_temp_3 <- in_talls_temp_3 + months(3) - days(1)

更多 time_point 秒。这可能会在未来增长......

我想到了一个“递归函数”? (这是它的正确名称吗?)像这样(只是一个想法):

    repeat_v <- function(x){
  n <-  length(x)
  
  for (y in 1:n) {
    return(x[[y]])
    
  }
  
}

我不知道使用 for 循环是否是正确的方法(应用会更好?)。我也怀疑这个想法,不知道它是否适合这份工作,否则我以后会后悔的,因为会很费时间..

有什么想法吗?

如有任何想法,我们将不胜感激! ^^

就这样:

generate_df <- function(months, time_points, min_val=200, max_val=500, 
                        from=i_preImp_preref, 
                        to=f_postImp) {
  dates <- seq.Date(from=from, to=to, by=paste0(months, " months"))
  data.frame(name = paste0("time_point", 1:time_points),
             a    = round(runif(length(name), min_val, max_val), 0),
             i.date.time.point = dates,
             f_talls_temp_3 = dates + months(months) - days(1))
}

第一个 df 会是这样的:

generate_df(6, 13, 200, 500, i_preImp_preref, postImp)

第二个:

generate_df(3, 13, 200, 500, i_preImp_preref, postImp)

我们可以只使用标准 R [ 子集:

n = readr::parse_number(data$name)
data$i.date.time.point = in_talls_temp_6[n]
data$f.date.time.point =  f_talls_temp_6[n]

#            name   a i.date.time.point f.date.time.point
# 1   time_point1 267        2014-07-01        2014-12-31
# 2   time_point2 208        2015-01-01        2015-06-30
# 3   time_point3 332        2015-07-01        2015-12-31
# 4   time_point4 325        2016-01-01        2016-06-30
# 5   time_point5 455        2016-07-01        2016-12-31
# 6   time_point6 345        2017-01-01        2017-06-30
# 7   time_point7 425        2017-07-01        2017-12-31
# 8   time_point8 212        2018-01-01        2018-06-30
# 9   time_point9 359        2018-07-01        2018-12-31
# 10 time_point10 297        2019-01-01        2019-06-30
# 11 time_point11 230        2019-07-01        2019-12-31
# 12 time_point12 334        2020-01-01        2020-06-30
# 13 time_point13 457        2020-07-01        2020-12-31