1

I am using the dataprep function of the Synth package (see https://cran.r-project.org/web/packages/Synth/Synth.pdf).

I have the following command in R that I want to loop:

dataprep.out <- dataprep(foo=expenditures,
     predictors=c("pop_tot", "share_yng", "share_old", "unempl", "sign_pop_initiative", "cab_size", "parl_size"),
     predictors.op="mean",
     dependent="exp_welf",
     unit.variable="canton",
     unit.names.variable="canton_abr",
     time.variable="year",
     treatment.identifier=7,
     controls.identifier=c(6, 8, 15, 16),
     time.optimize.ssr=1980:1995,
     time.plot=1980:2007,
     time.predictors.prior=1980:1995
     )

class(dataprep.out)
[1] "list"

So far so good. The result is a list[8]. Now I want to loop the command (note the changes in treatment.identifier and controls.identifier:

for (i in c(6,8,15,16)){dataprep(foo=expenditures,
                     predictors=c("pop_tot", "share_yng", "share_old", "unempl", "sign_pop_initiative", "cab_size", "parl_size"),
                     predictors.op="mean",
                     dependent="exp_welf",
                     unit.variable="canton",
                     unit.names.variable="canton_abr",
                     time.variable="year",
                     treatment.identifier=i,
                     controls.identifier=c(6,8,15,16)[c(6,8,15,16) !=i],
                     time.optimize.ssr=1980:1995,
                     time.plot=1980:2007,
                     time.predictors.prior=1980:1995)}

How do I save every iteration in it's own list? I'd like to have a list, e.g. dataprep.out"i", for every iteration and therefore every object "i" (dataprep.out6; dataprep.out8 etc.).

I guess I have to create those objects first so the loop can save its results into the corresponding list. Nevertheless I am struggling with what object I have to create exactly. And I dont' know how to tell the for loop to assign every result to a list that is varying only in "i" (dataprep.out"i", where "i" is varying).

Fusscreme
  • 152
  • 9

1 Answers1

1

You can create list before and then add item to it like

i_list=c(6,8,15,16)
result_list=list()
for (i in seq(i_list)){
  result_list[[i]]=dataprep(...)}

But your need to changes in identifier

treatment.identifier=i_list[[i]],
controls.identifier=c(6,8,15,16)[c(6,8,15,16) !=i_list[[i]]]

But lapply variant as @zx8754 told better

result_list=lapply(i_list,function(i) dataprep(...))
Batanichek
  • 7,761
  • 31
  • 49
  • I tried the lapply version and `result_list` is a `list[4]`. With `result_list[1]` I can access the result for i=6 and with `result_list[8]` the results for i=2. Is this correct? But I don't know how to access X1 as I could for `dataprep.out$X1`. – Fusscreme May 04 '16 at 12:51
  • 1
    try `result_list[[1]]` ( double [ for get first element)/ with X1 like `result_list[[1]]$X1` .... result_list[[1]] - return dataprep for 1-st element from your array c(6,8,15,16) – Batanichek May 04 '16 at 12:53
  • `result_list[[1]]$X1` worked! Thank you for the prompt answers to all my questions, sir. – Fusscreme May 04 '16 at 12:56