0

I am searching for a way in R to get the all numbers for integers from a function in a range from a to b

pfA <- function(x) {  return (  123 * x ) ; }
p_a = Vectorize(pfA, "x")
plot(0,0, col="purple", type="l", xlim=c(-5,5000), ylim=c(-5,5000))
curve (p_a(x), col="red", add=TRUE)
print_values((p_a(x),a,b)

where print_values would return a list (or two dimesional array), containing all x in the range a to b and the corresponding return value of the function

 1 : 123
 2 : 246
 3 : 369
...
0

1 Answer 1

2

I modified the code of the curve function to create myfun.

myfun <-
function (expr, from, to) 
{
  sexpr <- substitute(expr)
  x <- seq.int(from, to)
  ll <- list(x = x)
  y <- eval(sexpr, envir = ll)
  cbind(x = ll[["x"]], y)
}

Example usage:

a <- -5
b <- 5
myfun(p_a(x), a, b)

#        x    y
#  [1,] -5 -615
#  [2,] -4 -492
#  [3,] -3 -369
#  [4,] -2 -246
#  [5,] -1 -123
#  [6,]  0    0
#  [7,]  1  123
#  [8,]  2  246
#  [9,]  3  369
# [10,]  4  492
# [11,]  5  615
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.