>5 Console
5.1 R interface
In contrast to many other statistical software packages that use a point-and-click interface, e.g. SPSS, JMP, Stata, etc, R has a command-line interface. The command line has a command prompt, e.g. >, see below.
This means, that you will be entering commands on this command line and hitting enter to execute them, e.g.
help()Use the up arrow to recover past commands.
hepl()
help() # Use up arrow and fix5.2 R GUI (or RStudio)
Most likely, you are using a graphical user interface (GUI) and therefore, in addition, to the command line, you also have a windowed version of R with some point-and-click options, e.g. File, Edit, and Help.
In particular, there is an editor to create a new R script. So rather than entering commands on the command line, you will write commands in a script and then send those commands to the command line using Ctrl-R (PC) or Command-Enter (Mac).
a = 1
b = 2
a + b[1] 3
Multiple lines can be run in sequence by selecting them and then using Ctrl-R (PC) or Command-Enter (Mac).
5.3 Cut-and-paste
One of the most effective ways to use this documentation is to cut-and-paste the commands into a script and then execute them.
Cut-and-paste the following commands into a new script and then run those commands directly from the script using Ctrl-R (PC) or Command-Enter (Mac).
x <- 1:10
y <- rep(c(1,2), each=5)
m <- lm(y~x)
s <- summary(m)Now, look at the result of each line
x
y
m
s
s$r.squared5.4 Calculator
All basic calculator operations can be performed in R.
1+2[1] 3
1-2[1] -1
1/2[1] 0.5
1*2[1] 2
2^3 # same as 2**3[1] 8
For now, you can ignore the [1] at the beginning of the line, we’ll learn about that when we get to vectors.
5.4.1 Advanced calculator operations
Many advanced calculator operations are also available.
(1 + 3) * 2 + 100^2 # standard order of operations (PEMDAS)[1] 10008
sin(2 * pi) # the result is in scientific notation, i.e. -2.449294 x 10^-16 [1] -2.449294e-16
sqrt(4)[1] 2
log(10) # the default is base e[1] 2.302585
log(10, base = 10)[1] 1
5.5 Variables
A real advantage to using R rather than a calculator (or calculator app) is the ability to store quantities using variables.
a = 1
b = 2
a + b[1] 3
a - b[1] -1
a / b[1] 0.5
a * b[1] 2
b ^ 3[1] 8
5.5.1 Case sensitive
R is a case sensitive language and therefore you need to be careful about capitalization.
ThisObjectExists <- 3
ThisObjectExists[1] 3
thisobjectexists # no it doesn'tError: object 'thisobjectexists' not found
5.5.2 Valid object names
Valid object names “consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number”.
# Valid object names
a = 1
.b = 2# Invalid object names
2a = 3
.2a = 4
_c = 5Error in parse(text = input): <text>:2:2: unexpected symbol
1: # Invalid object names
2: 2a
^
You cannot use any reserved names as object names, i.e. these names cannot be overwritten.
?Reserved5.5.3 Tab auto-complete
Many graphical user interfaces (GUIs) and integrated development environments (IDEs) provide the use of tab to complete the names of variables and functions. If you start typing a function or variable name, you can hit the tab key and a collection of functions and variables that start with those characters will appear. This allows you to quickly select the variable or function name without having to type the complete name. Thus, you can use informative variable and function names without excess typing.
5.5.4 Assignment operators =, <-, and ->
When assigning variables values, you can also use arrows <- and -> and you will often see this in code, e.g.
a <- 1 # recommended
2 -> b # uncommon, but sometimes useful
c = 3 # similar to other languagesNow print them.
a[1] 1
b[1] 2
c[1] 3
5.5.5 Use informative variable names
While using variables alone is useful, it is much more useful to use informative variables names.
# Rectangle
length <- 4
width <- 3
area <- length * width
area[1] 12
perimeter <- 2 * (length + width)
# (Right) Triangle
opposite <- 1
angleDegrees <- 30
angleRadians <- angleDegrees * pi/180
(adjacent <- opposite / tan(angleRadians)) # = sqrt(3)[1] 1.732051
(hypotenuse <- opposite / sin(angleRadians)) # = 2[1] 2
5.6 Summary
The R Console is where all commands are executed and printed results are returned.