Objects Basics

R is an object oriented language. Every object must have a name. Object names must start with a letter, and can only contain letters, numbers, underscore (_) and period (.).

The two main kinds of objects are data and functions.

Data Objects Basics

To create a data object, give it a name, and assign it a numerical value, do this:

x <- 5

Here, x is the newly-created variable’s name and 5 is its current value. Note that 5 -> x and x = 5 would also have worked.

You don’t have to assign value. You can create a NULL object for later use:

y <- NULL

Putting a command within parentheses

One could force a command to print out its results to the screen by enclosing it in parentheses. When you execute the command x <- 25, R creates the object x, assigns it the value of 25, and then silently prompts you for the next command. But the command (x <- 25) does everything that x <- 25 does and in addition gives you on-screen output. Try the following:

(z <- 100)
## [1] 100

Types of data: numeric, character, logical

The value assigned to an object does not have to be of the numerical type, as in x <- 5. The assigned values could be of the character type – e.g., x <- "Donald Trump" – or of the logical type – e.g., x <- TRUE.

Functions Basics

You can check whether the object x exists:

exists("x")           # Note the quotation marks! Single quotes OK.
## [1] TRUE

You’ll get TRUE if x exists, and FALSE otherwise.

Here, exists() is an example of a function. You feed a function an object and it does something – presumably something useful – with the object.

That’s not to say that you necessarily have to feed a function an object for it to do something useful. For example, you can find the names of all currently defined objects:

ls()
## [1] "x" "y" "z"

If there are no currently defined objects, this returns character(0).

The command objects() is equivalent to ls().

Functions do need to be fed objects. But default objects may be baked into the construction of the function, in which case the function will use the default object if none is explicitly supplied. In ls(), if no directory is explicitly provided within parenthesis, the function uses the default directory, in this case the working directory. More on files and directories.

Information about object x can be obtained from the following functions:

class(x)
str(x)
summary(x)
typeof(x)

To remove an object x use this function: rm(x). (Now, exists("x") will return FALSE.)

Value can be assigned to an object directly, as in the examples above (as in x <- 5), or indirectly. The command exists("x") that we saw above returns data of the logical type, either TRUE or FALSE. So, we can do this indirect assignment: y <- exists("x").

Similarly, it is possible to assign the outputs of any function to an object: y <- ls(), y <- class(x), etc.

If you execute y <- rm(x), x gets removed and y takes the NULL value. If you execute y <- rm(y), y remains in the global environment, but with the NULL value.

You don’t need to pre-define an object before you feed it to a function. Consider this code chunk:

x <- 25
sqrt(x)
## [1] 5

The sqrt() function gives you the positive square root of a positive number. In this case, it returns 5 as the positive square root of 25. But we could have simply written sqrt(25) and gotten the same answer.

Similarly, abs(x) and abs(25) return the absolute value of x and the number 25, respectively.

The commands log(x = x, base = 10) and log(x = 25, base = 10) return the base-ten logarithm of x and the number 25, respectively.

Finding help on functions

The command ?log() will produce information on the log() function.

Functions and their arguments

The help page for the log() function lists the arguments of the function. The log() function has two arguments: x and base.

The x argument has no default value. So, the command log() will generate the following message: “Error: argument”x" is missing, with no default."

The base argument, however, does have a default value: e, which is 2.71828. So, if you provide no value for base, your log(x = 25) command – or, equivalently, your log(25) command – will generate the natural logarithm of 25.

And so will log(x = x), because the second x gets interpreted as “the value of the object x in the current working directory”, and currently its value happens to be 25.

Argument sequence

Consider the following commands:

log(x = 100, base = 10)   # 2
## [1] 2
log(base = 10, x = 100)   # Same as the previous command
## [1] 2
log(100, 10)              # Same as the previous command
## [1] 2
log(10, 100)              # Log of 10 to the base 100 or 0.5
## [1] 0.5

That is, if the arguments for the log function are not explicitly named, R assumes that the arguments’ values are listed in a very specific sequence: first value for argument x; second value for argument base.

There are numerous functions in R. Just you wait!

And it is possible to create one’s own functions! See this.