# Include these two commands at the beginning of your script. The first
# deletes everything in the workspace/environment, the second deletes
# everything in the console.
rm(list = ls()) # delete everything in workspace/environment
cat("\f") # delete everything in console
# To run a chunk of code, select it and press 'cmd+enter' on a Mac or click on
# the 'Run' button on top of the script-window.
# Comments are chunks of code (or text) that R ignores when running. They
# start with a hashtag, as above. To comment-out a chunk of code or text, on a
# Mac, select it and press 'cmd+shift+c'.
# To request help with a command, type the command with a question mark in
# front in the console. For example, '?sqrt' shows the help page for the
# command 'sqrt'.
# To create an object (a variable) called 'a' that is equal to three, use one
# of the following three commands:
= 3
a <- 3
a assign("a", 3)
# In the workspace window, you can see that a variable was created. To display
# it in the console, type
show(a) #or:
## [1] 3
print(a)
## [1] 3
# To delete it, type
rm(a) # rm stands for remove
# You can perform basic arithmetic operations with numbers as well as defined
# objects/variables:
3 + 8
7^2
= 3
a = 8
b + b # displays the result in console
a = a + b # saves the result to a new variable, 'c'
c
# Many standardly used functions are already implemented by default in R. For
# example:
sqrt(a) # square-root
log(a) # natural log (ln)
exp(a) # exponential
# trigonometric functions:
cos(a)
sin(a)
tan(a)
ceiling(3.4) # returns 4, the smallest integer larger than 3.4
floor(3.4) # returns 3, the largest integer smaller than 3.4
%%a #computes the modulus; remainder of b/a
b
# One can easily combine functions:
sqrt(ceiling(8.9))
log(exp(a))
# To round a number to a desired number of digits:
round(342.347, digits = 2)
# A variable can also take on the value 'Inf' or '-Inf' (positive or negative
# infinity). Any further operation with this variable gives again 'Inf' or
# '-Inf'. For example,
= Inf
a
sqrt(a) # gives Inf
-Inf + 4 # gives -Inf
# A variable can also take on the value 'NA' or 'NaN' (not-a-number). (This
# happens, for example, if one loads an excel file with an empty cell into R,
# as the value in such a cell needs to be distinguished from, say, zero.)
# Again, any operation with an NA returns NA:
= NA
a
sqrt(a) # gives NA
NA + 4 # gives NA
# (The same results are obtained when a is 'NaN' instead of 'NA'.)
# Objects you create are stored by R as different 'types'. Numbers like the
# ones created above are of type 'double' and 'numeric':
= 3
a = 8
b
typeof(a) # tells you the type of variable/object 'a'
is.double(a) # verifies whether a is of type double
is.numeric(a) # verifies whether a is a numeric
# The difference between a double and a numeric is typically irrelevant. (You
# can think of 'numeric' being a larger category that encompasses both 'double'
# and 'integer' objects.)
# to verify whether a is Inf or -Inf:
is.infinite(a)
# to verify whether a is NA:
is.na(a)
# to verify whether a is NaN:
is.nan(a)
# Throughout this tutorial, we'll discuss the types of different objects we
# create. It is important to be aware that there are different types that R
# uses to store objects because sometimes a command only works for a specific
# type (or gives different results for different types), in which case one
# needs to convert the object to the right type.
# A logical is an object-type that can take on the values 'TRUE', 'FALSE' or
# 'NA'. It indicates whether some condition is true or false (or cannot even
# be evaluated).
# Logicals are (usually) created using logical operators:
= a > b
isAlargerB show(isAlargerB)
# Other logical operators:
< b
a >= b
a <= b
a == b # equal to
a != b # not equal to
a
# Comparisons with Inf and NA:
< Inf # comparisons with Inf work
a < NA # but comparisons with NA yield NA
a
# Logicals can be used in standard computations, whereby 'TRUE' indicates a 1
# and 'FALSE' indicates a 0:
TRUE + 3 # gives 4
TRUE == 1 # gives TRUE
# Nevertheless, the 'double'-object 1 and the 'logical'-object TRUE are not the
# same thing because their type differs, i.e. R stores them differently:
= TRUE
a = 1
b
typeof(a)
typeof(b)
# To check whether some object is a logical or a double, type:
<- 1
isAlargerC
typeof(isAlargerB)
typeof(isAlargerC)
is.logical(isAlargerB)
is.logical(isAlargerC)
is.double(isAlargerC)
# To convert numbers of type double to logicals or vice versa:
as.logical(1) # convert number/double 1 into logical TRUE
as.double(TRUE) # opposite: convert logical TRUE into number/double 1
as.numeric(TRUE) # same
# Based on existing logicals (i.e. variables indicating whether a condition is
# true or false), further logicals can be created that combine these underlying
# conditions using special operators. For example:
= TRUE
isXtrue = FALSE
isYtrue
# negation of a logical: turns TRUE into FALSE (and FALSE into TRUE)
= !isXtrue
isXnottrue
# gives true only if both are true:
= isXtrue & isYtrue
areBothTrue # gives TRUE if only one of them is true and also if both are true:
= isXtrue | isYtrue
isOneTrue # gives TRUE only if onle one of them is true (it gives FALSE if both are true)
= xor(isXtrue, isYtrue)
isExactlyOneTrue
# (Note that these commands work even if you defined isXtrue and isYtrue as the
# double-types 1 and 0, respectively. Therefore, in this particular case, the
# object type is irrelevant.)
# Further below, we will discuss vectors of logicals (i.e. vectors whose
# elements are logicals rather than numbers (of type double)).
# Moreover, logicals will be important when we talk about indexing different
# objects in which data is stored (a vector, matrix, array, list or dataframe).
# A string is an object that contains text. It is stored as type 'character' in
# R.
= "banana"
a typeof(a) # returns 'character'
is.character(a)
# There are many commands that are specific to strings:
nchar(a) # number of characters in string
substr(a, 1, 3) # returns first three characters in string
# String Conversion:
as.character(3) # convert the number 3 to the string '3'
as.double("3") # opposite: convert the string '3' to the number (double) 3
as.numeric("3") # same
format(3, nsmall = 2) # convert the number 3 to the string '3.00'
# The function 'paste()' creates a string by combining a number (double) and a
# string, e.g.:
paste(1, "mouse", sep = ".") #returns '1.mouse'
paste(1, "mouse") # if option 'sep' is not specified, a space is put as separator
# String modification:
gsub("i", "X", "Mississippi") #replace i with X
# Strings allow for advanced printing commands, beyond 'print()':
print(a)
print(paste(a, "ice cream"))
# prints the specified string, inserting the number at '%s':
sprintf("C(X) = [0,%s]", 3.45)
sprintf("%.0f", 1e+08) # to avoid scientific notation
cat("hello\nworld\n") # prints the string, adding line-breaks at \n
# Further below, we will discuss vectors of strings (i.e. vectors whose
# elements are strings rather than numbers (of type double) or logicals).
# Moreover, strings will be important when we talk about indexing different
# objects in which data is stored (a vector, matrix, array, list or dataframe).
# They will also be important when we talk about representing dates.
# R is an open-source language, which means that there are many developers who
# write their own packages that define specific commands (functions) not
# included in R by default. To highlight which commands belong to which
# packages, we will load the packages only once these commands are needed.
# However, in actual applications, it is best practice to have an initial
# section where you load all packages. To load a package, it first needs to be
# installed. You can install a package by using the 'Packages' panel in
# bottom-right window, or by typing e.g.
install.packages("ggplot2") # to install package 'ggplot2'
# A package needs to be installed only once on a given computer (though you may
# want to re-install packages when a new version is available). In contrast,
# you have to load the package every time a script is ran anew. To load the
# package, type
library(ggplot2) # to load package 'ggplot2'