I am supposed to write a function for Stirling Numbers of the Second Kind, given by the formula:
For this, I have written the following function in R:
stirling <- function(n, k)
{
sum = 0
for (i in 0:k)
{
sum = sum + (-1)^(k - i) * choose(k, i) * i^n
}
sum = sum / factorial(k)
return(sum)
}
The next part of the question is to "create a plot for n = 20, k = 1,2,...,10". I did some research and I think the methods curve or plot might help me. However, I am guessing these methods are used when y is of the form f(x) (i.e. a single argument). But here, I have two arguments (n and k) in my function stirling so I am not sure how to approach this.
Also, I tried converting the values of k (0, 1, 2..., 10) to a vector and then passing them to stirling, but stirling won't accept vectors as input. I am not sure how to modify the code to make stirling accept vectors.
Any suggestions?

plot(1:10, Vectorize(stirling)(20, 1:10))apply()is suitable. For basic example,df <- expand.grid(n = 18:22, k = 1:10); res <- apply(df, 1, function(x) stirling(x[1], x[2])); df <- cbind(df, res)