Simple calculations

Try to perform simple computations like we already know from math. Keep in mind that laws of precedence are as usual

2+3
2+3*4

Assignment operation

Operator is “<-”. “=” also works but less common

x <- 2

y = 2

z <- 4

x+y*z

result <- x+y*z

result

Everything given a name is an object and is stored in the workspace.

ls()
objects()

Vectors

We can create simple vectors using different ways, either by giving specific range of numbers or by using the c() function

x <- 1:5
x
y <- 11:15
y

z <- c(10,6,3,5,1)
z

Indexing of vectors is done using the square brackets

y[2]

z[4]

y[2:4]

We can select subsets of vectors

z[c(5,1,2)]
z[(x==TRUE)] #Use logical

Seq function is useful for creating ranges and also looping. For every function you can either type help(function name) or function name? and you will see the way this function works

help(seq)
a <- seq(10,20)
a

a <- seq(20,10)
a

b <- seq(from=17,to=30,by=2)
b

b <- seq(17,30,2)
b

Here are some examples of functions that work on vectors. After you run them, be sure that you understand what they are doing

vec <- c(10,5,1,12,4)
vec

vec^2

vec + vec

sum(vec) # This is a common operation

mean(vec)

sum(vec)/length(vec) #Note R's flexibility

vec2 <- c(vec,10,10,4)
vec2

table(vec2)

We can use the “usual” comparison operators (==, <, >, <=, >=, !=), boolean operators ( |, & )and the sum() function to look for several things in vectors. Try the following examples and be sure that you understand what they are implementing.

vec2 == 1

sum(vec2 == 1)

vec2 == 10

sum(vec2 == 10)

vec2 > 1 & vec2 < 12 #Use & Boolean operator

length(vec2) #Very useful function for writing loops

Sometimes it is useful to check whether a variable is a vector or an object (or…). See how we can check if something is a vector or how we convert an object to a vector

is.vector(vec2)

#Convert an object to a vector
vec3 <- as.vector(vec2)

Matrices

If we want to go two-dimensional, there are many ways to do that and also many ways to handle these structures. Let’s start by creating arrays using the matrix function. Check how matrix function works and what are the parameters you need to define.

?matrix
x <- matrix(1:12,nrow=3)
x

#What does dim do?
dim(x)

Next we will see how we can convert one or more vectors to a matrix format. This operation is useful when we want to merge vectors. We will use cbind function (short for column bind). Check how cbind works. Similarly there is also rbind (row bind).

temp1 <- c(5,10,12,24,42,60,63,72)
temp2 <- c(8,1,3,88,33,0,77,42)

#cbind -- Good for matrices, works with data frames but can cause muchos problemos.
#each vector must be of same length.
temp_mat <- cbind(temp1, temp2)
temp_mat

is.matrix(temp_mat)

#rbind -- Row Bind, same logic as cbind but with rows. Each vector must be of same length.
temp_mat2 <- rbind(temp1,temp2)

Indexing works the same way as for vectors, but in this case pay attention that we need to define rows and columns. Pay attention on how can I say to R “take all rows”, or similary “take all columns”

temp_mat2[1,1:3]

temp_mat2[2,4]

temp_mat2[1:2,1:2]

temp_mat2[,1:4] #All rows, but just columns 1:4. This approach is very handy.

temp_mat2[1,] #Just row 1

Directories, loading and saving data

There are also ways on how to change working directory, load another working directory and list files of a directory.

# In Windows, slashes need to be around the other way. Can copy/paste from 
# Explorer address bar, then switch the slashes

setwd("/Users/gerasimos/Dropbox/MST_COURSES/SS16/practicals/")

getwd() #Look at directory

dir() #See what is in directory
list.files() #does same as above but takes longer

Next we will see how we can read and write data.

#Write out the temp_mat data
write.csv(temp_mat, "temp_mat.csv", row.names=FALSE)
dir()

temp_mat_new <- read.csv("temp_mat.csv")
#pay attention to the row.names argument. When is needed and when not.

class(temp_mat_new)
temp_mat_new
system("rm temp_mat.csv") # in Windows it is: shell("del temp_mat.csv")
dir() #It is removed now

Similarly, we can explort files to Excel using xlsx package.

library(xlsx)
xlsx.file <- "~/data/dummmyData.xlsx"
write.xlsx(temp_mat_new, xlsx.file, sheetName = "sheet1", row.names = F)
temp_mat_newest <- read.xlsx(xlsx.file, sheetName = "sheet1")
temp_mat_newest

There are even more packages on how to interact with databases (RODBC package) or the SAS program (foreign package).

Finally, let’s see how it is possible to save and load R objects. This is very useful when you want to save a specific object (variable) and then load it again. Or there is even the choice of saving your whole workspace and load it afterwards.

a <- 1:10
save(a, file = "dumData.Rdata")
rm(a)

load("dumData.Rdata")
a

#the following saves the workspace
save.image("dumData.Rdata")

#This statement clears the workspace
rm(list=ls())

load("dumData.Rdata")