function for fetching the values from criteria

i want to create a function where i can create a general variables like data from below table for example if i give grace_type = National then it will create general variables Grace 1, grace 2 m grce 3, grace 4 as (2,2,6,9)
the same way i want to give for continental , national, Rural.....

for example if i give global option as grace_type = National then
Grace 1 should automatically to 2
Grace 2 should automatically to 2
Grace 3 should automatically to 6
Grace 4 should automatically to 9



Continental International National Regional Rural No grace
Grace 1 3 4 2 3 4 0
Grace 2 5 4 2 3 4 0
Grace 3 7 8 6 4 4 0
Grace 4 10 12 9 8 7 0

Reading between the lines, it seems like you might be asking for plain variables to be created in your environment with the rownames, based on this table; the first step would be a simply dplyr::select, then I suspect you intend something like unpacking assignment that you can get from zealot package
https://cran.r-project.org/web/packages/zeallot/vignettes/unpacking-assignment.html

originally i don't want to fetch numbers from tables , i want to make global variables generally , tables is just for example

fetch from, implies a source; your source is a table or something else. if its a table then its as i said. you use select to narrow the table you want to get. then you can use zealot to assign the values.

I recommend you look at zealot

ohh my bad, now i have updated the question

In the end I decided not to use zealot, though it might be helpful, I think a table is still a good idea.

library(tidyverse)
my_getter <- function(col){
 mysource <-  tibble::tribble(
    ~var, ~Continental, ~International, ~National, ~Regional, ~Rural, ~`No grace`,
      "Grace 1",        3L,             4L,        2L,        3L,     4L,        0L,
    "Grace 2",         5L,             4L,        2L,        3L,     4L,        0L,
    "Grace 3",          7L,             8L,        6L,        4L,     4L,        0L,
    "Grace 4",         10L,            12L,        9L,        8L,     7L,        0L
    )
  
  
 to_assign <- select(mysource,
         var,any_of(col)) |> deframe()
 iwalk(to_assign,\(val,name)assign(x=name,
                                  value=val,
                                    envir=.GlobalEnv))

}

my_getter("National")
`Grace 1`
#2 
my_getter("International")
`Grace 1`
#4

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.