I have two functions, each of which return two values (as a list) and which operate over a limited domain:
# for x >= 0
f <- function(x) { return(list(a=x,b=2*x)) }
# for x < 0
g <- function(x) { return(list(a=x/2,b=x/4)) }
I want to do something like this:
fg <- function(x) { return(ifelse(x < 0, g(x), f(x))) }
And I'd like to get this:
> fg(c(1,2))
a$
[1] 1 2
b$
[2] 2 4
> fg(c(-1,-2)
a$
[1] -0.5 -1.0
b$
[2] -0.25 -0.50
Instead, I am getting this:
> fg(c(1,2))
[[1]]
[1] 1 2
[[2]]
[1] 2 4
> fg(c(-1,-2))
[[1]]
[1] -0.5 -1.0
[[2]]
[1] -0.25 -0.50
The "$a" and "$b" labels are getting lost.
With a single input, it's even worse:
> fg(1)
[[1]]
[1] 1
What am I doing wrong?
How can I achieve my objective?