How does the following code work? I got the example when I was reading the help line of R ?curve
. But i have not understood this.
for(ll in c("", "x", "y", "xy"))
curve(log(1+x), 1, 100, log = ll,
sub = paste("log= '", ll, "'", sep = ""))
Particularly , I am accustomed to numeric values as arguments inside the for-loop
as,
for(ll in 1:10)
But what is the following command saying:
for(ll in c("","x","y","xy"))
c("","x","y","xy")
looks like a string vector? How does c("","x","y","xy")
work inside curve
function as log(1+x)
[what is x here? the string "x"? in c("","x","y","xy")
] and log=ll
?
Apparently, there are no answers on stack overflow about how the curve
function in R works and especially about the log
argument so this might be a good chance to delve into it a bit more (I liked the question btw):
First of all the easy part:
c("","x","y","xy")
is a string vector or more formally a character vector.
for(ll in c("","x","y","xy"))
will start a loop of 4 iterations and each time ll
will be '','x','y','xy'
respectively. Unfortunately, the way this example is built you will only see the last one plotted which is for ll = 'xy'
.
Let's dive into the source code of the curve
function to answer the rest:
x
represent in log(1+x)
? log(1+x)
is a function. x
represents a vector of numbers that gets created inside the curve
function in the following part (from source code):
x <- exp(seq.int(log(from), log(to), length.out = n)) #if the log argument is 'x' or
x <- seq.int(from, to, length.out = n) #if the log argument is not 'x'
#in our case from and to are 1 and 100 respectively
As long as the n
argument is the default the x
vector will contain 101 elements. Obviously the x
in log(1+x)
is totally different to the 'x' in the log
argument.
as for y
it is always created as (from source code):
y <- eval(expr, envir = ll, enclos = parent.frame()) #where expr is in this case log(1+x), the others are not important to analyse now.
#i.e. you get a y value for each x value on the x vector which was calculated just previously
log
argument? The log
argument decides which of the x
or y
axis will be logged. The x-axis if 'x'
is the log
argument, y-axis if 'y'
is the log
argument, both axis if 'xy'
is the log
argument and no log-scale if the log
argument is ''
.
It needs to be mentioned here that the log of either x or y axis is being calculated in the plot
function in the curve
function, that is the curve
function is only a wrapper for the plot
function.
Having said the above this is why if the log
argument is 'x' (see above) the exponential of the log values of the vector x are calculated so that they will return to the logged ones inside the plot
function.
PS the source code for the curve
function can be seen with typing graphics::curve
on the console.
I hope this makes a bit of sense now!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.