79425991

Date: 2025-02-10 02:51:02
Score: 1
Natty:
Report link

Similar logic to https://stackoverflow.com/a/14502298/4868692, instead of constructing call itself, something that is a bit opaque, we utilize the well-known function do.call.

  1. [ is a function with variable number of arguments depending on x.
  2. do.call is typically used to call functions with constructed argumnets.
dimget = function(x, dim, idx, drop = TRUE){
    d = rep(list(TRUE), length(dim(x)))
    d[[dim]] = idx
    do.call(`[`, c(list(x), d, drop = drop))
    }

Advantages:

  1. Probably safer, no non-standard evaluation going on in here.
  2. Easier to understand since we are just making a list of arguments instead of constructing call, no quote, bquote or as.call makes the code easier to read and understand even for simpletons like me. Otherwise, the code is de-facto identical.

Disadvantages:

  1. Performance? We are constructing a list, but not modifying any elements of it. I would say no memory allocation should happen, but it is hard to say without benchmarking.
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (0.5):
Posted by: Colombo