0

I have a dataframe created from a loop. The loop examines ordinal regression for a couple dozen outcomes for a given exposure.

At the beginning of the loop, a variable called exposure is defined. Example: exposure <- "MyExposure"

At the end of the routine, I want to actually save the resulting data set I've compiled and to have the name of the saved data object be related to the exposure.

I've had issues with making the left hand side of the assignment based on the variable names.

The name of the new dataframe should be

paste0(exposure,"_imputed_ds")
[1] "MyExposure_imputed_ds"

However, when I try to put this on left hand of an assignment, it fails.

paste0(exposure,"_imputed_ds") <- existing.data.frame
Error in paste0(exposure,"_imputed_ds") <- existing.data.frame
  could not find function "paste0<-"

What I wanted was a new dataframe named MyExposure_imputed_ds that contained contents of existing.data.frame

akaDrHouse
  • 2,190
  • 2
  • 20
  • 29
  • After more searching, i found this post http://stackoverflow.com/questions/31225715/how-can-i-change-the-name-of-a-data-frame that details using assign. As I'm typing this, I see that @arvi1000 has provided the same info. Thank you Arvi1000. – akaDrHouse Apr 19 '17 at 16:57

1 Answers1

4

You can use assign() to set a value for a name you construct with paste

assign(paste0('MyExposure', '_imputed_ds'), 5)

Now you have MyExposure_imputed_ds in the environment with value 5

I find the use of assign to be generally a warning flag, though! Maybe you want something like this instead...

imputed_ds <- list()
imputed_ds[['MyExposure']] <- 5
arvi1000
  • 9,393
  • 2
  • 42
  • 52
  • (and of course here I'm just using `5` as an arbitrary value; you could do the same with a data.frame or anything else too) – arvi1000 Apr 19 '17 at 16:57