Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dynamic Variables for List Key

Trying to learn R, and I’m getting an idea of lists. I have a function I’m calling:

try_dives <- function(fns, r) {
    D1 = function(r) round(r ** 2 + 100, 3)
    dn = function(r) round(r + 100, 3)

    for (fn in fns) {
        if (fn == "D1") {
            print(list('D1' = sapply(r, D1)))
        }
        # ... if / else ladder for D1, D2 ... dn
        else if (fn == 'dn') {
            print(list('dn' = sapply(r, dn)))
        }
    }
}

And call it as

try_dives(c('D1', 'D2', 'dn'), 100:200)

This, to me seems real inefficient, I would think I could just set the list variable

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

list(fn = sapply(r, fn)) and call the appropriate function where fn == 'D1', ...etc...

>Solution :

If the functions are defined outside, we can call those functions in the main function by looping over the fns with lapply and then call the function on r with match.fun and return the output as a named list by naming the list with setNames

try_dives <- function(fns, r) {
    setNames(lapply(fns, function(fn) match.fun(fn)(r)), fns)


}

-testing

D1 <- function(r) round(r ** 2 + 100, 3)
dn <- function(r) round(r + 100, 3)
try_dives(c("D1", "dn"), 100:200)
$D1
  [1] 10100 10301 10504 10709 10916 11125 11336 11549 11764 11981 12200 12421 12644 12869 13096 13325 13556 13789 14024
 [20] 14261 14500 14741 14984 15229 15476 15725 15976 16229 16484 16741 17000 17261 17524 17789 18056 18325 18596 18869
 [39] 19144 19421 19700 19981 20264 20549 20836 21125 21416 21709 22004 22301 22600 22901 23204 23509 23816 24125 24436
 [58] 24749 25064 25381 25700 26021 26344 26669 26996 27325 27656 27989 28324 28661 29000 29341 29684 30029 30376 30725
 [77] 31076 31429 31784 32141 32500 32861 33224 33589 33956 34325 34696 35069 35444 35821 36200 36581 36964 37349 37736
 [96] 38125 38516 38909 39304 39701 40100

$dn
  [1] 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
 [30] 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
 [59] 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
 [88] 287 288 289 290 291 292 293 294 295 296 297 298 299 300

Or another option is to create a named list` of functions within the function and then subset the function list based on the input names passed in ‘fns’ and apply over the vector

try_dives <- function(fns, r) {
    fnlist <- list(D1 = function(r) round(r ** 2 + 100, 3), 
          dn = function(r) round(r + 100, 3))
  lapply(fnlist[fns], \(fn) fn(r))


}

-testing

> try_dives(c("D1", "dn"), 100:200)
$D1
  [1] 10100 10301 10504 10709 10916 11125 11336 11549 11764 11981 12200 12421 12644 12869 13096 13325 13556 13789 14024
 [20] 14261 14500 14741 14984 15229 15476 15725 15976 16229 16484 16741 17000 17261 17524 17789 18056 18325 18596 18869
 [39] 19144 19421 19700 19981 20264 20549 20836 21125 21416 21709 22004 22301 22600 22901 23204 23509 23816 24125 24436
 [58] 24749 25064 25381 25700 26021 26344 26669 26996 27325 27656 27989 28324 28661 29000 29341 29684 30029 30376 30725
 [77] 31076 31429 31784 32141 32500 32861 33224 33589 33956 34325 34696 35069 35444 35821 36200 36581 36964 37349 37736
 [96] 38125 38516 38909 39304 39701 40100

$dn
  [1] 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
 [30] 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
 [59] 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
 [88] 287 288 289 290 291 292 293 294 295 296 297 298 299 300
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading