0

I want to save based on a variable.

t="Cis" d="1083"

For this run, I had to use the following to create my named dataset based on these assigned variables.

assign(paste0(t,"_",d,"_","tcpl.pod"), data.frame(c1,c2,c3,c4,c5))

This worked and created my dataframe with name = Cis_1083_tcpl.pod This is important to me, because I will want to loop through different values for t and d.

Question 1: I always hear "AVOID USING ASSIGN!" Is there a better way of achieving the above?

Question 2: I want to save that object and still refer to it based on the coding as follows:

save(paste0(t,"_",d,"_","tcpl.pod"), file = paste0(t,"_",d,"tcpl.pod.RData"))

This gives error that the 'paste0(t,"_",d,"tcpl.pod.RData"))' not found.

How do I refer to the first paste construction appropriately in R?

P.S. save(get(paste0(t,"_",d,"_","tcpl.pod")), file = paste0(t,"_",d,"tcpl.pod.RData")) returns the same error.

akaDrHouse
  • 1,781
  • 2
  • 15
  • 22
  • 1
    Is there a better way? Use a [list of data frames instead](https://stackoverflow.com/q/17499013/903061). No `assign` needed, and it's easy to use the results. – Gregor Thomas Jun 01 '17 at 19:35
  • For the save part, you'll probably need to use something like `save(get(paste0(t,"_",d,"_","tcpl.pod")), file = paste0(t,"_",d,"tcpl.pod.RData"))` – Matt Jewett Jun 01 '17 at 19:36
  • And `get` is what you want if you stick with the `assign(paste())` idiom - marking as dupe. – Gregor Thomas Jun 01 '17 at 19:37
  • @MattJewett I had tried get. It does not work. Same error. – akaDrHouse Jun 01 '17 at 19:37
  • You should also look at `?save`, to use character strings with `save` use the `list` argument: `save(list = paste0(t,"_",d,"_","tcpl.pod"), file = paste0(t,"_",d,"tcpl.pod.RData"))` – Gregor Thomas Jun 01 '17 at 19:39
  • @Gregor Can you please provide a link to the question this is a duplicate of. Or one that illustrates your list provision, which I don't understand for my second question at all. – akaDrHouse Jun 01 '17 at 19:40
  • @Gregor Thanks, you were providing as I was typing. – akaDrHouse Jun 01 '17 at 19:40
  • For the list version, you would want to setup a blank list outside of the for loop using something like `my.list – Matt Jewett Jun 01 '17 at 19:51
  • Thanks to both of you! – akaDrHouse Jun 01 '17 at 19:57
  • It looks like the save method I gave might not work. You can use this to save out each dataframe though `df.to.save – Matt Jewett Jun 01 '17 at 20:02
  • You're doing it wrong if you're pasting together names of an already-created list. To save each element of the list separately, `lapply(names(my.list), function(x) saveRDS(my.list[[x]], file = paste0(x, ".RDS")))`. Or just save the whole list... – Gregor Thomas Jun 01 '17 at 20:18
  • Gentlemen, I am confused as hell about lists. I still can't get it to name the element of the list using my "tokens". I want the NAME of the data.frame, whether it is a single item or in a list to be defined with tokens in `paste0( t,"_",d,"tcpl.pod.RData"))`. I then want to save a .csv and a .RData file by referring to that same token string. I must be missing something because I can't infer that from either of the links (the duplicate) or the link in first comment. – akaDrHouse Jun 01 '17 at 21:51
  • It's not clear what code you are running / why you can't generalize because you haven't posted a small, reproducible example. Work up a *simple* example. You don't need `t` and `d`, just pick one variable, let it take 2 values, and create a 2 or 3 row data frame with it. Post the complete code for that example and maybe we can re-open the question and find the problem. – Gregor Thomas Jun 02 '17 at 21:24

0 Answers0