Home > Net >  Recursive function over a vector based on the length of it
Recursive function over a vector based on the length of it

Time:06-01

I have several periods separated by 6 months. Initial "in_talls_temp_6" and Ends "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)

I have data like this:

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

And I want to assign to each "time_point" x their initial and ending dates of the period.

Until now I have done it the silly way:

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])
          )

Getting this:

               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
    

I think that there is a better way and I'm not capable of doing it. I'm stucked here cause I want to get bigger with the project and now I want to do the same with:

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)

More time_points. And this probably could grow in the future...

I have thought about a ¿recursive function? (is this the proper name to it?) like this (just an idea):

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

I dunno If it's the right way to do it is with a for loop (apply would be better?). Also I doubt with the idea and don't know if it's good for the job or I will regret it later because will be time consuming..

Any ideas?

Any thoughts will be appreciated! ^^

CodePudding user response:

Just do:

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))
}

The first df would be sth like:

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

And the second:

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

CodePudding user response:

We can just use standard R [ subsetting:

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
  • Related