rwdvc
rwdvc

Reputation: 457

Efficient Way to Build Data Frame / Data Table

I have a data.frame that I am using to set parameters for simulations.

states_grid <- expand.grid(years = c(1:47), start_pct = c(0:99), sim_num = c(1:50))

The above code creates all the states that I would like to simulate. My issue becomes creating a data.frame to hold the outputs. What I would like to do is to create a larger data frame in which we add in an ob_num variable. The ob_num variable will run from 1 to the number of years indicated in column 1.

For example:

   years start_pct sim_num ob_num
1:     2        99       1      1
2:     2        99       1      2
3:     3        99       1      1
4:     3        99       1      2
5:     3        99       1      3
6:     4        99       1      1
7:     4        99       1      2
8:     4        99       1      3
9:     4        99       1      4

However I can't think of an efficient way to create this data frame.

Thoughts?

Edit: I tried the below suggestion but that didn't seem to do it.

The below code returns a data.table of the same size (235,000) rows.

states_grid <- expand.grid(years = c(1:(year_max - year_min + 1)),
                           start_pct = c(0:99),
                           sim_num = c(1:50))
states_grid <- data.table(states_grid)
setDT(states_grid)[, ob_num := 1:.N, by = years][]

I also tried:

states_grid <- setDT(states_grid)[, ob_num := 1:.N, by = years][]

Both methods return 235K rows.

Upvotes: 1

Views: 119

Answers (1)

eddi
eddi

Reputation: 49448

CJ(years = c(1:47), start_pct = c(0:99), sim_num = c(1:50))[,
   .(ob_num = seq_len(years)), by = .(years, start_pct, sim_num)]
#         years start_pct sim_num ob_num
#      1:     1         0       1      1
#      2:     1         0       2      1
#      3:     1         0       3      1
#      4:     1         0       4      1
#      5:     1         0       5      1
#     ---                               
#5639996:    47        99      50     43
#5639997:    47        99      50     44
#5639998:    47        99      50     45
#5639999:    47        99      50     46
#5640000:    47        99      50     47

Upvotes: 1

Related Questions