Functions

Functions

Watch this video:

Run a function

Can you use the sqrt() function in the chunk below to compute the square root of 962?

sqrt(962)

Code

Use the code chunk below to examine the code that sqrt() runs.

sqrt

Good job! sqrt immediately triggers a low level algorithm optimized for performance, so there is not much code to see.

lm

Compare the code in sqrt() to the code in another R function, lm(). Examine lm()’s code body in the chunk below.

lm

Help pages

Wow! lm() runs a lot of code. What does it do? Open the help page for lm() in the chunk below and find out.

?lm

Good job! lm() is R’s function for fitting basic linear models. No wonder it runs so much code.

Code comments

What do you think the chunk below will return? Run it and see. The result should be nothing. R will not run anything on a line after a # symbol. This is useful because it lets you write human readable comments in your code: just place the comments after a #. Now delete the # and re-run the chunk. You should see a result.

sqrt(962)

Next topic