Search code examples
rstatisticspopulation

finding different y values along curve in r


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.


Solution

  • 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