Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

Each plus-sign is a continuation prompt.

It just indicates that a line continues


from
the preceding line.
And here’s how to use the function:
> sumofsquares(3,4,5)
[1] 50
Comments
A comment is a way of annotating code. Begin a comment with the # symbol, which
of course is an octothorpe. (What’s that you say? “Hashtag”? Surely you jest.) This
symbol tells R to ignore everything to the right of it.
Comments are very helpful for someone who has to read code that you’ve written.
For example:
sumofsquares <- function(x,y,z){ # list the arguments
sumsq <- sum(c(x^2,y^2,z^2)) # perform the operations
return(sumsq) # return the value
}
Just a heads-up: I don’t add comments to lines of code in this book. Instead, I
provide detailed descriptions. In a book like this, I feel that’s the most
effective
way to get the message across.
As you might imagine, writing R functions can encompass WAY more than I’ve
laid out here. To learn more, check out R For Dummies, by Andrie de Vries and Joris
Meys (John Wiley & Sons).
R Structures
I mention in the “R Functions” section, earlier in this chapter, that an R function
can have many arguments. It’s also the case that an R function can have many
outputs. To understand the possible outputs (and inputs, too), you must under-
stand the structures that R works with.

You might also like