2

I'm having problems with applying a function to a vector of arguments. The point is, none of the arguments are vectors.

I'm trying to apply my function with the do.call command, and my attempts go like this:

do.call("bezmulti", list(dat$t, as.list(getvarnames(n, "a"))))

where bezmulti is a function that takes in a vector (dat$t) and an indefinite number of single numbers, which are provided by the function getvarnames in the form of a vector, which I need to split.

The problem is that this list doesn't work the way I want it to - the way I would want would be:

[[1]]
#vector goes here
[[2]]
#the
[[3]]
#numbers
[[4]]
#go
[[5]]
#here

however my proposed solution, and all my other solutions provide lists that are either somehow nested or have only two elements, both of which are vectors. Is there a way to force the list to be in the format above?

EDIT: Functions used in this post look as follows

bezmulti <- function(t,...) {
  coeff <- list(...)
  n <- length(coeff)-1
  sumco <- rep(0, length(t))
  for (i in c(0:n)) {
    sumco=sumco+coeff[[i+1]]*choose(n, i)*(1-t)^(n-i)*t^i
  }
  return(sumco)
}

getvarnames <- function(n, charasd) {
  vec=NULL
  for (j in c(1:n)) {
    vec <- append(vec, eval(str2expression(paste0(charasd, as.character(j)))))
  }
  return(vec)
}
  • 1
    Welcome to StackOverflow! Can you provide`bezmulti`? and `dat$t`? You can check [how to do that here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – hrvg May 19 '22 at 21:25
  • We also don't know `getvarnames()`! – jay.sf May 19 '22 at 21:26

1 Answers1

1

I think what you need to do is this:

do.call("bezmulti", c(list(dat$t), as.list(getvarnames(n, "a"))))

For example:

dat= data.frame(t = c(1,2,3,4,6))
c(list(dat$t), as.list(c(8,10,12)))

Output:

[[1]]
[1] 1 2 3 4 6

[[2]]
[1] 8

[[3]]
[1] 10

[[4]]
[1] 12
langtang
  • 22,248
  • 1
  • 12
  • 27