Objects and Functions
An object is a thing – like a number, a dataset, a summary statistic like a mean or standard deviation, or a statistical test. Objects come in many different shapes and sizes in R. There are simple objects which represent single numbers, vectors which represent several numbers, more complex objects like dataframes which represent tables of data, and even more complex objects like hypothesis tests or regression which contain all sorts of statistical information.
Let’s try defining an object here. In your console, type the following:
We did a few things here. First, we defined our object (
list
), and then gave our object a list of associated observations (1-5). We also used some functions. But first let’s define what a function is.
A function is a procedure that typically takes one or more objects as arguments (aka, inputs), does something with those objects, then returns a new object. For example, the
mean()
function we used above takes a vector object, likelist
, of numeric data as an argument, calculates the arithmetic mean of those data, then returns a single number (a scalar) as a result. R has hundreds (thousands?) of built-in functions that perform most of the basic analysis tasks you can think of. You can also create your own!
Works Cited
- Phillips, Nathanial D. YaRrr! The Pirate’s Guide to R