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
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.
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.