running a function on each cell in a column of values then assigning each one to a column of object names without creating each one separately

So I have an data table like so:

I want to create a series of plots, but don't want to do it individually each time.

I was wondering if there is a way to take the value in column 2 row 1, run a function on it and then assign that to an object by naming that object with the text in column 1 row 1 . . .
and then do this for each subsequent row without writing it out for each row

right now for each row I would do this:

ID101 <- qr_code('https://www.bbc.com/news/world-us-canada-68075637') %>%
plot()

Is there a way to write a piece of code that would take a value from column 2, run a function on it, end then assign the result to the ID in column 1?

Here I'm actually creating QR codes using the package qrcode, but that doesn't matter to my question, I just want to know if there is a way to do this for any function/plot that I need to repeat one hundred times.

tidyverse/purrr has tooling for applying functions in an iterative fashion.

example

library(tidyverse) # or at least library(purrr)

df_ <- data.frame(x=LETTERS[1:3],
y=c(10, 20, 30))

thing_to_do <- function(b){
  b*b
}

walk2(df_$x, df_$y,
       \(x_for_name, y_for_value){ 
         assign(x = x_for_name,
                value = thing_to_do(y_for_value),
                envir = .GlobalEnv)
         }
       )

A
B
C

Note that in general I wouldnt want to pollute the global environment with however many names; prefer to make another explicit environment, or store the results in a simple list, but you asked for global environment so thats what I did here.

1 Like

THANK YOU!!!!

This is perfect. I am learning as I go and haven't really taken a course or anything, so was a little stumped. Usually I just search online, but my question was so convoluted and I wasn't really sure what terms to search for.

Thanks a lot :slight_smile:

that does something ... multiples a number by itself.
this is a demonstration after all. I assume you didnt really need to square numbers.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.