-1

So I have plotted a curve, and have had a look in both my book and on stack but can not seem to find any code to instruct R to tell me the value of y when along curve at 70 x.

curve(
20*1.05^x, 
from=0, to=140, 
xlab='Time passed since 1890', 
ylab='Population of Salmon', 
main='Growth of Salmon since 1890'
)

So in short, I would like to know how to command R to give me the number of salmon at 70 years, and at other times.

Edit: To clarify, I was curious how to command R to show multiple Y values for X at an increase of 5.

  • yes, but I was wondering if there was a way to command R to output the values. for Say every 5 years. Rather then use my calculator (which I know how to do) Was just trying to see whether R had the ability which I would have assumed it does. – SverigeKile Oct 09 '16 at 02:13
  • You don't command R. Ask it nicely. – Hack-R Oct 09 '16 at 03:23

3 Answers3

0

curve is plotting the function 20*1.05^x so just plug any value you want in that function instead of x, e.g.

> 20*1.05^70
[1] 608.5285
>
0
20*1.05^(seq(from=0, to=70, by=10))

Was all I had to do, I had forgotten until Ed posted his reply that I could type a function directly into R.

0
salmon <- data.frame(curve(
  20*1.05^x, 
  from=0, to=140, 
  xlab='Time passed since 1890', 
  ylab='Population of Salmon', 
  main='Growth of Salmon since 1890'
))
salmon$y[salmon$x==70]

1 608.5285

This salmon data.frame gives you all of the data.

head(salmon)
    x        y
1 0.0 20.00000
2 1.4 21.41386
3 2.8 22.92768
4 4.2 24.54851
5 5.6 26.28392
6 7.0 28.14201

If you can also use inequalities to check the number of salmon in given ranges using the syntax above.

It's also simple to answer the 2nd part of your question using this object:

salmon$z <- salmon$y*5 # I am using * instead of + to make the plot more clear

plot(x=salmon$x,y=salmon$z, xlab='Time passed since 1890', ylab='Population of Salmon',type="l")
lines(salmon$x,salmon$y, col="blue")

enter image description here

Hack-R
  • 22,422
  • 14
  • 75
  • 131