how to create a vector from function outputs

#1
Hi everyone,

I'm a complete beginner with R and am trying to use it for my maths project, however I'm getting stuck with what I reckon are some quite elementary things, just because I've never used it before and am teaching myself.

This is what I have so far and what I am doing:

Code:
popr = vector(mode='numeric', 100)
> popr
  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 [69] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
This is a vector which represents a population - it's set to zero at the moment but this is what I will vary eventually.

This is my function:

Code:
π <- function(S)
+ {x <- sum(popr[0:S])
+ return((100-S)*x)}
What I want to do, is create a vector
Code:
Y = (π(1), π(2), .... , π(100))
so that I can form the following:

Code:
av.payoffd <- popd%*%Y
And for the life of me I can't work out how; I've tried things like this:

Code:
> Y[0:100] = π(1):π(100)
> Y[0:100] = (π(1:100))
And I can see why they don't work, because π(1), π(2), .... , π(100) isn't a sequence like 1:100...
I guess I want the set {π(i) s.t. i is in [1,100]} and then to turn that into a vector?
Is there a better way of defining my function so I can do that?

If anyone can help me out with how to do this, that would be amazing! At the moment I'm resorting to entering all 100 entries manually but this is really inelegant and also annoying because you have to redefine every time popr changes - which it will do!

Thanks so much for your help :)

Lucy
 
Last edited:
#3
Hi, thanks for the tip re. code tags. I've tried to add in a bit more about the function..I'm very happy to write more but I don't want to overload with unnecessary info! so let me know if there's any other info I should include.

As for the 1-based arrays, should that affect my problem? I had a quick google and couldn't see how it would?
 

Dason

Ambassador to the humans
#4
I meant that using 0 as an index doesn't do what you think it might so you should change that in your code.

Code:
> x <- c(3, 2, 9)
> x[0]
numeric(0)
> x[1]
[1] 3
> x[0:3]
[1] 3 2 9
> x[1:3]
[1] 3 2 9