How do I get a parameter in a chart label?

I'm sure you can help me: I created some function that depends on some parameters (like n). Then I plot the density and I want to tag it with n entered as a function parameter. To be more specific:

plot(function(x) dnorm(x,0,1), main="n=...")
lines(density(y), col = 'red')

      

I want "..." to be 100 automatically when I used my function with n = 100 (instead of always editing the function).

Thanks in advance!

+3


source to share


1 answer


Just use paste0()

:

n <- 100
plot(function(x) dnorm(x,0,1), main=paste0("n=",n))
lines(density(y), col = 'red')

      



  • paste0()

    a little more convenient here, as it paste()

    uses the default split
  • main=sprintf(n=%d",n)

    should work as well
+3


source







All Articles