Problem pasting numbers into new dataframe name, etc. in a for loop

20 Views Asked by At

This is a very basic question, but I'm new to R and using loops in general. I have imputed some data using the mice package and would like to pull out each of the m = 20 imputed datasets for some supplementary analyses on each.

I can do this with the following code:

imputed.prison.1 <- complete(imputed.datasets.prison, 1)

This gives me a dataframe that corresponds to the first of the 20 imputed datasets. I'd like to repeat this for the other 19 using a for loop, and tried to do so using the following:

for(datnum in 1:20){
  paste0("imputed.prison.", datnum) <- complete(imputed.datasets.prison, datnum)
  }

Unfortunately, I get the error:

Error in paste0("imputed.prison.", datnum) <- complete(imputed.datasets.prison,  : 
  target of assignment expands to non-language object

I obviously cannot provide the data here, but this seems like a fairly basic problem and I'll need to do similar things at other points in my data preparation and analysis. If there is some general way to iterate over those numbers 1:20 at various points in my code, I would appreciate knowing it.

0

There are 0 best solutions below