0

I'm trying to use a for loop to create a set of dynamic objects in R. These will contain a list of organisations and values against a certain metric--each output will be the values of an individual metric.

In practice, this will be used to create chart objects using ggplot2, which I'll then use in RMarkdown. For the example below, it's just a sample using a head() function for each metric.

I tried using the paste function to create this name, but it gives the following error:

Error in paste("organisation_short", "_", MetricIDs[x]) <- head(organisationdata_Jan2021) : target of assignment expands to non-language object

I understand that the assign function might help, but I'm not sure how to use it. (My attempts also produced errors). I found a similar question in the link below, but it's set up in a way that pipes data directly into assign. I'm also not clear what "value = ." is doing. This query is below: dynamically name objects in R

I believe the "value = ." refers to the data being piped into the assign function. I created an alternative version which is in the code below.

Error in assign(x = organisationdata_Jan2021, value = paste0("sampledata", : invalid first argument

The idea is to create output files along the lines of: organisation_short_ABC123, organisation_short_ABC323, organisation_short_KJM088

I would be grateful for any guidance you might have!

    MetricIDs <- c('ABC123','ABC323','KJM088')

# Attempt using paste
    for (x in 1:3) 
      {
      organisationdata_Jan2021 <- organisationdata_CM0040_Jan2021 %>% filter(Metric_ID==MetricIDs[x])  # Filter data to specific Metric ID
      paste("organisation_short","_", MetricIDs[x]) <- head(organisationdata_Jan2021)  # Goal: Create object that includes the Metric ID.
      }

# Attempt using assign 
for (x in 1:3) 
{
  organisationdata_Jan2021 <- organisationdata_CM0040_Jan2021 %>% filter(Metric_ID==MetricIDs[x])  # Filter data to specific Metric ID
  assign(x=organisationdata_Jan2021, value=paste0("sampledata",MetricIDs[x])) 
}

    
    
    # Expected object names:  organisation_short_ABC123, organisation_short_ABC323, organisation_short_KJM088
    # This will be used to create chart objects using ggplot2, and those objects will be used in an R MarkDown document.
T PERRY
  • 81
  • 7
  • 1
    `assign(paste0("organisation_short","_", MetricIDs[x]), head(organisationdata_Jan2021)` should do it. – DaveArmstrong Jul 05 '22 at 15:27
  • 1
    Relevant if using `assign`: https://stackoverflow.com/questions/17559390/why-is-using-assign-bad – Harrison Jones Jul 05 '22 at 15:29
  • 1
    I might be completely off, but if you want to eventually loop over the variables you created again to create the image files, it might be simpler to create a list where each items name is your organisation and metric ID as in `results[[paste0("organisation_short_", MetricIDs[x])]] <- whatever`. That way, you don't have to generate all the names again but can just iterate over the names of the list items. – clemenskuehn Jul 05 '22 at 16:31
  • Thank you all! Yes, I'm seeing that working with lists is the best way forward here. I haven't previously worked with them so this is a learning point for me. @clemenskuehn, do you know whether a list as you suggested could hold a ggplot object? If so, that would work brilliantly. – T PERRY Jul 06 '22 at 08:48
  • Hi @DaveArmstrong, I tried your approach, but it still gave me the error I got when using paste directly for the field name. Have I got something wrong in the assignment statement? ``` for (x in 1:3) { organisationdata_Jan2021 <- standrewsdata_CM0040_Jan2021 %>% filter(Metric_ID==MetricIDs[x]) # Filter data to specific Metric ID assign(paste0("organisation_short","_", MetricIDs[x])) <- organisationdata_Jan2021 } ``` Error in assign(paste0("organisation_short", "_", MetricIDs[x])) <- organisationdata_Jan2021 : target of assignment expands to non-language object – T PERRY Jul 06 '22 at 09:27
  • The `assign()` statement should look like this: `assign(paste0("organisation_short","", MetricIDs[x]), organisationdata_Jan2021) ` – DaveArmstrong Jul 06 '22 at 12:13
  • Hey @TPERRY. A list can hold a ggplot object, but it an get a little tricky because the plots already inside the list might get updated by the next one you add ([see here](https://stackoverflow.com/questions/31993704/storing-ggplot-objects-in-a-list-from-within-loop-in-r) ). But if you want to write images/plots to files, you don't necessarily have to store them in a list first. Maybe it's sensible to consider filtering the data, creating the plot and writing the plot to a file all in the same loop? If you add a little more data and plot to your question, cewrtainly someone will make it work – clemenskuehn Jul 06 '22 at 20:05

0 Answers0