# Write your code here
# Using # before your code produced a comment not a code. Putting 4 #s produces a section heading in your script.
# Use ? before a function to get help
#### 1.1 Introduction ####
2+2
## [1] 4
print("Hello World")
## [1] "Hello World"
sin(90)
## [1] 0.8939967
?c
#### 1.2 Simple Analysis With RStudio ####
employed <- c(60.323, 61.122, 60.171, 61.187, 63.221, 63.639)
GNP <- c(234.289, 259.426, 258.054, 284.599, 328.975, 346.999)
plot(employed,GNP, pch=21 , col="red" , bg="yellow") # Plot with options

cor.test(employed,GNP) # Correlation test
##
## Pearson's product-moment correlation
##
## data: employed and GNP
## t = 7.2639, df = 4, p-value = 0.001908
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.7012562 0.9962071
## sample estimates:
## cor
## 0.9641231
#### 2.1 Creating Vectors ####
1:7 # Create a sequence from 1 by 1 to 7
## [1] 1 2 3 4 5 6 7
c(6,7,1:5) # Combine methods of creating vectors
## [1] 6 7 1 2 3 4 5
seq(from=3, to=11, by=2) # Use the sequence function
## [1] 3 5 7 9 11
rep(1:3, each=3, times=2) # Create vectors using the repeat function
## [1] 1 1 1 2 2 2 3 3 3 1 1 1 2 2 2 3 3 3
rep(1:3, each=2, times=3)
## [1] 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3
1/1:7 # Preform operations on vectors
## [1] 1.0000000 0.5000000 0.3333333 0.2500000 0.2000000 0.1666667 0.1428571
(x<-seq(1,20,2)) # Create a vector x. Round brackets in the code print x after assignment
## [1] 1 3 5 7 9 11 13 15 17 19
(y<-x+1/x) # Create y from x.
## [1] 2.000000 3.333333 5.200000 7.142857 9.111111 11.090909 13.076923
## [8] 15.066667 17.058824 19.052632
log(y) # let a function apply on y component-wise
## [1] 0.6931472 1.2039728 1.6486586 1.9661129 2.2094947 2.4061258 2.5708491
## [8] 2.7124848 2.8366676 2.9472052
y<-round(y,digits=2) # Round y up to 2 decimal place
sort(y, decreasing = T) # sort y in a decreasing order
## [1] 19.05 17.06 15.07 13.08 11.09 9.11 7.14 5.20 3.33 2.00
x+y; x*y # Operations between two vectors are component-wise. First line is x+y, second line is x*y
## [1] 3.00 6.33 10.20 14.14 18.11 22.09 26.08 30.07 34.06 38.05
## [1] 2.00 9.99 26.00 49.98 81.99 121.99 170.04 226.05 290.02 361.95
#### 2.2 Dictionaries ####
Inventory<-c(pens = 20, pencils = 5, rulers=4,12) # Create a vector where each entry has a name
Inventory # Final entry has no name
## pens pencils rulers
## 20 5 4 12
Inventory<-c(20, 5, 4, 12) # Alternative way of doing the same operation using the function 'names'
names(Inventory)<-c("pens","penciles","rulers"," ") # " " quotation marks make a string from the input
# as opposed to a variable
Inventory["pens"] # Putting 1 set of square brackets takes the coloumn corresponding to pens
## pens
## 20
# Inventory[1] is the alternative code for the same operation
Inventory[["pens"]] # Putting two sets of square brackets takes the value corresponding to pens
## [1] 20
# Inventory[[1]] is the alternative code for the same operation
#### 2.3 Subsetting Vectors ####
(x<-1:4); (y<-seq(2,10,2)) # Create the vectors x, y
## [1] 1 2 3 4
## [1] 2 4 6 8 10
length(y) # Number of elements of y
## [1] 5
y[2] # Take the second entry of y
## [1] 4
y[c(1,2,3)] # Take the first, second, and third entry of y
## [1] 2 4 6
y[-c(1,2)] # Take all entries apart from first and second
## [1] 6 8 10
y[y>3] # Take all entries greater than 3
## [1] 4 6 8 10
which(y>3) # Which entries are greater than 3
## [1] 2 3 4 5
c(x,y) # Combine x and y
## [1] 1 2 3 4 2 4 6 8 10
#### 2.4 Matrices ####
(z<-1:6) # Create the vector z to be used for creating matrices
## [1] 1 2 3 4 5 6
(mat<-matrix(z,nrow = 2)) # Create a matrix from z which has two rows arranged by columns
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
(mat1<-matrix(z,nrow = 2, byrow = T)) # Use byrow = T to arrange the matrix by rows
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
(mat2<-matrix(z,ncol = 2, byrow = T)) # Create a matrix from z which has two columns arranged by rows
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
## [3,] 5 6
rownames(mat)<-c("oh","my") # Assign row names for mat
colnames(mat)<-c("lions","tigers","bears") # Assign column names for mat
mat # See the results
## lions tigers bears
## oh 1 3 5
## my 2 4 6
dim(mat) # See the dimensions of your matrix
## [1] 2 3
dimnames(mat) # See the dimensions names of mat
## [[1]]
## [1] "oh" "my"
##
## [[2]]
## [1] "lions" "tigers" "bears"
#### 2.5 Manipulating Matrices ####
mat[1,1] # Take the element in first row and first column
## [1] 1
mat[2,] # Take the second row
## lions tigers bears
## 2 4 6
mat[,c(1,3)] # Take the first and third column
## lions bears
## oh 1 5
## my 2 6
mat["my",c(1,3)] # Take a row by name and first and third column
## lions bears
## 2 6
sqrt(mat) # Functions apply on matrecies element-wise
## lions tigers bears
## oh 1.000000 1.732051 2.236068
## my 1.414214 2.000000 2.449490
mat%*%mat2 # Matrix multiplication use %*% (* alone prefoms an element-wise multiplication)
## [,1] [,2]
## oh 35 44
## my 44 56
rbind(mat,mat1) # Bind by row two matrecies
## lions tigers bears
## oh 1 3 5
## my 2 4 6
## 1 2 3
## 4 5 6
cbind(mat,mat1) # Bind by column two matrecies
## lions tigers bears
## oh 1 3 5 1 2 3
## my 2 4 6 4 5 6
#### 2.6 Data Types ####
v1<-1:12 # Create an integer vector
v2<-sqrt(v1) # Create a numeric vector
v3<-c("Conservative","Labour","Labour","Conservative") # Create a vector or strings
v4<-c(TRUE, FALSE) # Create a logical vector
class(v1) # Check the class of each vector
## [1] "integer"
class(v2)
## [1] "numeric"
class(v3)
## [1] "character"
class(v4)
## [1] "logical"
(fac<-factor(v3)) # A vector can be turned into a set of categorical variables called factors
## [1] Conservative Labour Labour Conservative
## Levels: Conservative Labour
levels(v3) # Factors have levels
## NULL
is.factor(v3); is.numeric(v1);is.logical(v3) # You can check if a variable has a given type
## [1] FALSE
## [1] TRUE
## [1] FALSE
table(fac) # check how many of a given factor there is in a vector by using the table()
## fac
## Conservative Labour
## 2 2
(age<-runif(25)*50) # You can create factors by grouping your data. runif() is for uniform distribution
## [1] 7.273399 27.372719 41.219117 16.202429 35.468760 30.524252 28.559289
## [8] 36.551544 14.993035 49.978960 2.144004 43.547389 5.438861 8.192110
## [15] 28.223134 9.094146 8.316498 36.965149 9.606285 11.777707 35.620681
## [22] 2.574762 31.157525 15.048039 41.549646
(facs<-cut(age,c(0,10,20,30,40,50))) # Use the cut function to group your data into intervals
## [1] (0,10] (20,30] (40,50] (10,20] (30,40] (30,40] (20,30] (30,40]
## [9] (10,20] (40,50] (0,10] (40,50] (0,10] (0,10] (20,30] (0,10]
## [17] (0,10] (30,40] (0,10] (10,20] (30,40] (0,10] (30,40] (10,20]
## [25] (40,50]
## Levels: (0,10] (10,20] (20,30] (30,40] (40,50]
is.factor(facs) # Check the factor
## [1] TRUE
table(facs) # Count how many of a factor there is
## facs
## (0,10] (10,20] (20,30] (30,40] (40,50]
## 8 4 3 6 4
as.character(facs) # You can change the class of a vector by using as. function and check the class using is.
## [1] "(0,10]" "(20,30]" "(40,50]" "(10,20]" "(30,40]" "(30,40]" "(20,30]"
## [8] "(30,40]" "(10,20]" "(40,50]" "(0,10]" "(40,50]" "(0,10]" "(0,10]"
## [15] "(20,30]" "(0,10]" "(0,10]" "(30,40]" "(0,10]" "(10,20]" "(30,40]"
## [22] "(0,10]" "(30,40]" "(10,20]" "(40,50]"
as.factor(v1)
## [1] 1 2 3 4 5 6 7 8 9 10 11 12
## Levels: 1 2 3 4 5 6 7 8 9 10 11 12
as.factor(v1)+2 # You cannot use a factor as numeric
## Warning in Ops.factor(as.factor(v1), 2): '+' not meaningful for factors
## [1] NA NA NA NA NA NA NA NA NA NA NA NA
#### 2.6 Working with Strings ####
paste(1:3, c("cat","dog"), sep=" ") # Use the paste() function to print together elements of vectors
## [1] "1 cat" "2 dog" "3 cat"
paste(1:3, c("cat","dog"), sep=" ", collapse = "-" ) # Use collapse option to print everthing as one string
## [1] "1 cat-2 dog-3 cat"
v<-c("Conservative","Labour","Labour","Conservative","Liberal","UKIP") # Create a vector of strings
c("Labour","BNP") %in% v # Check if v contains elements
## [1] TRUE FALSE
match(v, c("Labour","Conservative")) # Which ones contain elements
## [1] 2 1 1 2 NA NA
substring(v,1,3) # Use substring to select the first 3 letters of each entry
## [1] "Con" "Lab" "Lab" "Con" "Lib" "UKI"
grepl("L",v) # Check which strings contain "L"
## [1] FALSE TRUE TRUE FALSE TRUE FALSE
gsub("Lab","New Lab",v) # Replace lab with New Lab in v
## [1] "Conservative" "New Labour" "New Labour" "Conservative"
## [5] "Liberal" "UKIP"
#### 2.7 Data Frames ####
sex <- c("Male", "Female", "Male", "Male", "Female", "Female", "Male", "Female") # Create columns
entry <- c(865,1042,962,759,967,829,909,948)
leaving <- c(1002,1172,998,781,1068,848,914,998)
length <- leaving-entry
censor <- c(0,1,1,1,NA,0,0,1)
(study <- data.frame(sex,entry,leaving,length,censor)) # Use data.frame() function to create a data frame
## sex entry leaving length censor
## 1 Male 865 1002 137 0
## 2 Female 1042 1172 130 1
## 3 Male 962 998 36 1
## 4 Male 759 781 22 1
## 5 Female 967 1068 101 NA
## 6 Female 829 848 19 0
## 7 Male 909 914 5 0
## 8 Female 948 998 50 1
nrow(study);ncol(study) # Check how many rows and columns
## [1] 8
## [1] 5
rownames(study);colnames(study) # Check row names and column names
## [1] "1" "2" "3" "4" "5" "6" "7" "8"
## [1] "sex" "entry" "leaving" "length" "censor"
study$entry; study[[2]]; study[,2] # Selecting a column as a row 3 different ways
## [1] 865 1042 962 759 967 829 909 948
## [1] 865 1042 962 759 967 829 909 948
## [1] 865 1042 962 759 967 829 909 948
study[-c(3:8),c(2,3)] # Selecting rows and columns as a data frame
## entry leaving
## 1 865 1002
## 2 1042 1172
class(study$sex) # Check the class of column
## [1] "factor"
class(study$censor)
## [1] "numeric"
study$censor<-factor(study$censor) # Change class of a column
str(study) # See structural information about your data farme
## 'data.frame': 8 obs. of 5 variables:
## $ sex : Factor w/ 2 levels "Female","Male": 2 1 2 2 1 1 2 1
## $ entry : num 865 1042 962 759 967 ...
## $ leaving: num 1002 1172 998 781 1068 ...
## $ length : num 137 130 36 22 101 19 5 50
## $ censor : Factor w/ 2 levels "0","1": 1 2 2 2 NA 1 1 2
(studymale <- subset(study, sex=="Male")) # Subset the data frame using the subset() function
## sex entry leaving length censor
## 1 Male 865 1002 137 0
## 3 Male 962 998 36 1
## 4 Male 759 781 22 1
## 7 Male 909 914 5 0
(studyages <- subset(study, sex=="Male", select = c(entry,leaving)))
## entry leaving
## 1 865 1002
## 3 962 998
## 4 759 781
## 7 909 914
#### 2.8 Lists ####
Mylist<-list("Hello World",Inventory,z,mat,study) # Create a list of some of the objects we have worked with so far
names(Mylist)<-c("Hello World","Inventory Dictionary","z used to creat mat" ,"mat the matrix","study the data frame")
Mylist
## $`Hello World`
## [1] "Hello World"
##
## $`Inventory Dictionary`
## pens penciles rulers
## 20 5 4 12
##
## $`z used to creat mat`
## [1] 1 2 3 4 5 6
##
## $`mat the matrix`
## lions tigers bears
## oh 1 3 5
## my 2 4 6
##
## $`study the data frame`
## sex entry leaving length censor
## 1 Male 865 1002 137 0
## 2 Female 1042 1172 130 1
## 3 Male 962 998 36 1
## 4 Male 759 781 22 1
## 5 Female 967 1068 101 <NA>
## 6 Female 829 848 19 0
## 7 Male 909 914 5 0
## 8 Female 948 998 50 1
length(Mylist) # How many elements your list has
## [1] 5
str(Mylist) # See information about the list
## List of 5
## $ Hello World : chr "Hello World"
## $ Inventory Dictionary: Named num [1:4] 20 5 4 12
## ..- attr(*, "names")= chr [1:4] "pens" "penciles" "rulers" " "
## $ z used to creat mat : int [1:6] 1 2 3 4 5 6
## $ mat the matrix : int [1:2, 1:3] 1 2 3 4 5 6
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : chr [1:2] "oh" "my"
## .. ..$ : chr [1:3] "lions" "tigers" "bears"
## $ study the data frame:'data.frame': 8 obs. of 5 variables:
## ..$ sex : Factor w/ 2 levels "Female","Male": 2 1 2 2 1 1 2 1
## ..$ entry : num [1:8] 865 1042 962 759 967 ...
## ..$ leaving: num [1:8] 1002 1172 998 781 1068 ...
## ..$ length : num [1:8] 137 130 36 22 101 19 5 50
## ..$ censor : Factor w/ 2 levels "0","1": 1 2 2 2 NA 1 1 2
Mylist$"mat the matrix" # Call an element of you list. Alternatively use
## lions tigers bears
## oh 1 3 5
## my 2 4 6
Mylist[4]; Mylist[[4]]; Mylist["mat the matrix"]; Mylist[["mat the matrix"]]
## $`mat the matrix`
## lions tigers bears
## oh 1 3 5
## my 2 4 6
## lions tigers bears
## oh 1 3 5
## my 2 4 6
## $`mat the matrix`
## lions tigers bears
## oh 1 3 5
## my 2 4 6
## lions tigers bears
## oh 1 3 5
## my 2 4 6
##### 3. Importing and Exporting Data #####
x<-scan() # The function scan() allows you to entre data into a vector
#2.4 3.6 1.1 4.2 NA