4 Let’s start with R

If you open R in your computer, you have a Console window which shows communication between you and computer. You can give commands directly in the console widow but it is reasonable to have a script which you can save, edit and run either partly or totally.

In this document you can see communications with R within grey boxes. Outputs are made as comments lead by ## symbols, so if you select the grey text and paste to your R Console, you should get own output. Are your outputs and commented ones identical?

Sometimes I have added own comments after # symbol.

4.1 R can work as a calculator

You can use parenteheses

5+7
## [1] 12
(5+7)/10
## [1] 1.2
(5+7)/10-3*(4-3)
## [1] -1.8

2.5 + 1.2 # Note that decimals are given with "." (not with ",")
## [1] 3.7

Examine what effects have arrows up and down (↑ & ↓). Examine whether spaces between commands matter, e.g. 5+7 vs. 5 + 7 vs. 5+ 7 etc

Answer ↑ & ↓ will navigate across your previous commands. Spaces does not matter!

4.2 Objects

Usually you need to save values to objects which can be indicated by words. Please note that there are differences between lower and upper cases, A is not equal to a.

a=5+7 # NB! You do not have the result of 5+7 in your Console window!
a # Now calling the object with name "a"
## [1] 12

A # As you can see, there is an error message about A!
## Error in eval(expr, envir, enclos): object 'A' not found

A=a/10
a
## [1] 12
A
## [1] 1.2

a=a+1 # mathematically not possible but in programming a common thing! The New A is the old A + 1
a
## [1] 13

my.var.1=(5+7)/10-3*(4-3)
my.var.1 # It is useful to use object names which help you to remember it!
## [1] -1.8

Save current year and the year of Estonian independence as objects and make a third object where you calculate current age of the Estonian Republic.

Answer
year.now=2019
year.Estonian.Republic=1918
Age=year.now-year.Estonian.Republic
Age
## [1] 101

4.3 Assignements with arrows

Many R users use assignment with arrows <- and -> instead of =.

b<-3.8 
b
## [1] 3.8
4->c # works in reverse order as well!
c
## [1] 4

It is up to you whether to use = or <-. I personally prefer = since I have used it in other programming languages, because it is shorter, and because the outcome is independent on spaces within the script.

Explore what is outocme if you type a space between each symbol in command c<-5

Answer
c < - 5
## [1] FALSE
# FALSE since c is NOT smaller than minus 5!

4.4 Functions

R is using functions to manipulate objects Functions are short words with parentheses where you can specify input object(s) and settings. There is a good help system with ?.

sqrt(9) # square root
## [1] 3
log10(a) # log based on 10
## [1] 1.113943
max(a,b,c)
## [1] 13
min(a,b,c)
## [1] 3.8
sum(a,b,c)
## [1] 20.8

Explore help by typing ?log and find a function for log(x+1). Explore waht happens if you use LOG(a). Why?

Answer
log1p(0)
## [1] 0
LOG(a)  # functions are also case-sensitive!
## Error in LOG(a): could not find function "LOG"

4.5 Object modes

R can use different data types, for example, numeric, character, logical etc.

mode(a)
## [1] "numeric"
my.text="UT"
mode(my.text)
## [1] "character"
my.logical=a==12
my.logical
## [1] FALSE
mode(my.logical)
## [1] "logical"
my.logical1=F # you can use T for TRUE and F for FALSE!
my.logical1
## [1] FALSE

Note that == means testing if objects are equal! Logical comparisons can also be <, >, <=, >=, and != (not equal).

The ! mark reverses the logical!

Explore what value you can get with !my.logical

Answer
!my.logical
## [1] TRUE
# Since ! reverses the logical value!

You can combine logical comparisons with & (and), | (or), use parentheses if needed.

You can try converting between classes by using commands as.numeric(), as.character() etc. Try to convert a to text and my.logical to numeric.

Answer
as.character(a)
## [1] "13"
as.numeric(my.logical)
## [1] 0
as.numeric(!my.logical)
## [1] 1
# TRUE will be 1 and FALSE will be 0!

4.6 Data dimensions

So far we explored single numeric values. R has other dimensions, for example vectors and matrices.

my.vec1=1:6 # a vector from 1 to 5
my.vec1
## [1] 1 2 3 4 5 6
my.vec2=c(a,b,c,1.2,5.3,2.2) # c is a function combine!
my.vec2
## [1] 13.0  3.8  4.0  1.2  5.3  2.2

my.matrix=matrix(my.vec2,nrow=3,ncol=2) # nrow and ncol are parameters
my.matrix
##      [,1] [,2]
## [1,] 13.0  1.2
## [2,]  3.8  5.3
## [3,]  4.0  2.2

length(my.vec2)
## [1] 6
nrow(my.matrix)
## [1] 3
dim(my.matrix) # number of rows and number of columns
## [1] 3 2

seq(from=1,to=6,by=0.2) # more detailed sequences
##  [1] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6
## [20] 4.8 5.0 5.2 5.4 5.6 5.8 6.0
seq(from=4,to=6,length.out=6)
## [1] 4.0 4.4 4.8 5.2 5.6 6.0
rev(my.vec2) # reversing
## [1]  2.2  5.3  1.2  4.0  3.8 13.0
rep(5,times=3) # repeating
## [1] 5 5 5
rep(1:2,each=3)
## [1] 1 1 1 2 2 2

What happens if you try to combine my.vec2 and my.text into a single vector?

Answer
c(my.vec2,my.text) # all numbers will be converted to text!
## [1] "13"  "3.8" "4"   "1.2" "5.3" "2.2" "UT"

4.7 Manipulating vectors and matrices

You can apply most commands to each element of an array or a matrix, or find a value based the whole data set. Manipulating two vectors or matrices of same dimensions makes calculations on each element separately

my.vec2+2
## [1] 15.0  5.8  6.0  3.2  7.3  4.2
(my.matrix+2)/my.vec2
##          [,1]     [,2]
## [1,] 1.153846 2.666667
## [2,] 1.526316 1.377358
## [3,] 1.500000 1.909091

# finding minimum, mean etc. from a vector or matrix
min(my.vec2)
## [1] 1.2
mean(my.vec2)
## [1] 4.916667
round(my.vec2)
## [1] 13  4  4  1  5  2

my.vec2+c(0,0,0,100,100,100)
## [1]  13.0   3.8   4.0 101.2 105.3 102.2

my.vec2>5
## [1]  TRUE FALSE FALSE FALSE  TRUE FALSE
my.vec2>5 & my.vec1<3  # both need to be true!
## [1]  TRUE FALSE FALSE FALSE FALSE FALSE

any(my.vec2>10) 
## [1] TRUE
all(my.vec2>1)
## [1] TRUE

rowSums(my.matrix)
## [1] 14.2  9.1  6.2
colSums(my.matrix)
## [1] 20.8  8.7

Write command which counts the number of cases my.vec2 <= 3.

Answer
sum(my.vec2<=3) # since all TRUE values are summed as 1-s we can easily sum logical vectors
## [1] 2

4.8 Accessing vectors and matrices

You can access a single value from a vector or matrix with numeric or named index. You need to put index between [ and ].

my.vec2
## [1] 13.0  3.8  4.0  1.2  5.3  2.2
my.vec2[1]
## [1] 13
my.vec2[4]
## [1] 1.2

my.vec2[1]=30   # replacing a single value!
my.vec2
## [1] 30.0  3.8  4.0  1.2  5.3  2.2

my.vec2[c(2,4)]=c(22,23) # putting to position 2 value 22 and to position 4 value 23
my.vec2
## [1] 30.0 22.0  4.0 23.0  5.3  2.2

# Using a logical vector to make something

my.vec2>10 # where values are > 10
## [1]  TRUE  TRUE FALSE  TRUE FALSE FALSE

my.vec2[my.vec2>10]=my.vec2[my.vec2>10]/2 # Replacing in my.vec2 all values > 10 with same values divided by 2
my.vec2
## [1] 15.0 11.0  4.0 11.5  5.3  2.2

my.matrix[1,1] # first row number and then column number (in R always in this order, row then column)
## [1] 13

names(my.vec2)=c("a","b","c","d","e","f") # assigning names
my.vec2
##    a    b    c    d    e    f 
## 15.0 11.0  4.0 11.5  5.3  2.2
my.vec2["c"] 
## c 
## 4
my.vec2[3] # you can still use numbers as well!
## c 
## 4


colnames(my.matrix)=c("c1","c2")
row.names(my.matrix)=c("r1","r2","r3")
my.matrix
##      c1  c2
## r1 13.0 1.2
## r2  3.8 5.3
## r3  4.0 2.2
my.matrix["r2","c2"]
## [1] 5.3
my.matrix[2,2]
## [1] 5.3

which(my.vec2>5) # note that numbers are numerical indices not values!
## a b d e 
## 1 2 4 5

my.vec2
##    a    b    c    d    e    f 
## 15.0 11.0  4.0 11.5  5.3  2.2
sort(my.vec2) # note names now!
##    f    c    e    b    d    a 
##  2.2  4.0  5.3 11.0 11.5 15.0
order(my.vec2) # indexes from low to high values
## [1] 6 3 5 2 4 1

my.vec2[order(my.vec2)]
##    f    c    e    b    d    a 
##  2.2  4.0  5.3 11.0 11.5 15.0

What must be object v that expressions v[v]==v is TRUE?

Answer
v=1
v[v]==v
## [1] TRUE

4.9 Specific values like NA, Inf or -Inf

NA indicates values “not available”. NaN is not a number, and Inf and -Inf means infinitive large or small value.

t=c(1:5,"text")
t
## [1] "1"    "2"    "3"    "4"    "5"    "text"
tn=as.numeric(t) 
## Warning: NAs introduced by coercion
tn # text not converted
## [1]  1  2  3  4  5 NA

4/0
## [1] Inf
4/0 > 9999999999999999999999999999999
## [1] TRUE

Explore logical function is.na, use vector tn and make a new numeric vector where NA is replaced by zero.

Answer
is.na(tn)
## [1] FALSE FALSE FALSE FALSE FALSE  TRUE

tn[is.na(tn)]=0 # Using logical vector
tn
## [1] 1 2 3 4 5 0

4.10 First figures


diversity=c(18,27,10,22,25,8,12)
hist(diversity)


soil.ph=c(4.5,6.4,3.9,5.3,5.9,3.2,4.2)

plot(soil.ph,diversity)

plot(soil.ph,diversity,xlab="Soil pH",ylab="Plant diversity") # axis labels

plot(soil.ph,diversity,xlab="Soil pH",ylab="Plant diversity",pch=16,col="red") # symbol code and color



soil.type=c("mineral","mineral","peat","mineral","mineral","peat","peat")

boxplot(diversity~soil.type) # here we give used parameters as formula y ~ x


color=rep("blue",length(diversity))
color[soil.type=="peat"]="brown"
plot(diversity
     ~soil.ph,xlab="Soil pH",ylab="Plant diversity",pch=16,col=color) # with formula!
legend("topleft",legend=c("peat","mineral"),pch=16,col=c("brown","blue"))
title("My first nice plot!")

Explore plot function and parameters pch, col and cex. Make a plot with different pch, col and cex parameters you like most.

Answer

pch is defining plot symbols, col is changing colors and cex is defining size of symbols.

4.11 Ending session

There is a function to end session: q(), but mostly you can just close the window. If R asks if we should save all objects to memory, I recommend saying No. We rather save later what we need.