Session 1 covered introduction to R data types and import/export of data.
R stores data in five main data types.
We can access and change information in our data.types using indexing with [] (or [[]] for lists).
## [1] 10
## [1] 1 2 3 4 5 6 7 8 9 0
For matricies and data frames we use two indexes, specifying columns and rows. matrix[rowIndex,columnIndex]
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
## [3,] 5 6
## [4,] 7 8
## [5,] 9 0
## [1] 1 2
## [1] 1 3 5 7 9
## [,1] [,2]
## [1,] 0 100
## [2,] 3 100
## [3,] 5 100
## [4,] 7 100
## [5,] 9 100
Remember a matrix can only contain one data type (numeric, character etc). For mixed data types we use data frames.
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
## [3,] 5 6
## [4,] 7 8
## [5,] 9 0
## [,1] [,2]
## [1,] "1" "Word"
## [2,] "3" "4"
## [3,] "5" "6"
## [4,] "7" "8"
## [5,] "9" "0"
We can use logical operations to evaluate information our data types.
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
aFactor <- factor(c("R","Python","R","R","Python"),levels = c("R","Python"))
aDataFrame <- data.frame(Number=c(1,2,3,4,5),Factor=aFactor)
aDataFrame
## Number Factor
## 1 1 R
## 2 2 Python
## 3 3 R
## 4 4 R
## 5 5 Python
## [1] TRUE FALSE TRUE TRUE FALSE
We can use logical operations with indexing to subset or alter information held in our data types.
## [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE
## [1] 1 2 3 4 5 10 10 10 10 10
When replacing factor columns in data frames we have to remember the levels.
aFactor <- factor(c("R","Python","R","R","Python"),
levels = c("R","Python"))
aDataFrame <- data.frame(Number=c(1,2,3,4,5),
Factor=aFactor)
aDataFrame[aDataFrame$Factor == "R",2] <- "NotPython"
## Warning in `[<-.factor`(`*tmp*`, iseq, value = c("NotPython", "NotPython", :
## invalid factor level, NA generated
## Number Factor
## 1 1 <NA>
## 2 2 Python
## 3 3 <NA>
## 4 4 <NA>
## 5 5 Python
We can update levels in a data frame column as we would a factor. Now we can replace our elements in data frame.
aDataFrame <- data.frame(Number=c(1,2,3,4,5),
Factor=aFactor)
aDataFrame$Factor <- factor(aDataFrame$Factor,
levels = c("R","Python","NotPython"))
aDataFrame[aDataFrame$Factor == "R",2] <- "NotPython"
aDataFrame
## Number Factor
## 1 1 NotPython
## 2 2 Python
## 3 3 NotPython
## 4 4 NotPython
## 5 5 Python
Data can be read into R as a table with the read.table() function and written to file with the write.table() function.
## Sample_1.hi Sample_2.hi Sample_3.hi Sample_4.low Sample_5.low
## Gene_a 4.570237 3.230467 3.351827 3.930877 4.098247
## Gene_b 3.561733 3.632285 3.587523 4.185287 1.380976
## Gene_c 3.797274 2.874462 4.016916 4.175772 1.988263
## Sample_1.low
## Gene_a 4.418726
## Gene_b 5.936990
## Gene_c 3.780917
We have looked at using logical vectors as a way to index other data types.
## [1] 1 2 3
Logicals are also used in controlling how scripted procedures execute.
While I’m analysing data, if I need to execute complex statistical procedures on the data I will use R else I will use a calculator.
Conditional branching is the evaluation of a logical to determine whether a chunk of code is executed.
In R, we use the if statement with the logical to be evaluated in () and dependent code to be executed in {}.
## x is true
More often, we construct the logical value within () itself. This can be termed the condition.
## The value of x is 10 which is greater than 4
The message is printed above because x is greater than y.
x is now no longer greater than y, so no message is printed.
We really still want a message telling us what was the result of the condition.
If we want to perform an operation when the condition is false we can follow the if() statement with an else statement.
x <- 3
if(x < 5){
message(x, " is less than to 5")
}else{
message(x," is greater than or equal to 5")
}
## 3 is less than to 5
With the addition of the else statement, when x is not less than 5 the code following the else statement is executed.
x <- 10
if(x < 5){
message(x, " is less than 5")
}else{
message(x," is greater than or equal to 5")
}
## 10 is greater than or equal to 5
We may wish to execute different procedures under multiple conditions. This can be controlled in R using the else if() following an initial if() statement.
x <- 5
if(x > 5){
message(x," is greater than 5")
}else if(x == 5){
message(x," is 5")
}else{
message(x, " is less than 5")
}
## 5 is 5
A useful function to evaluate conditional statements over vectors is the ifelse() function.
## [1] 1 2 3 4 5 6 7 8 9 10
The ifelse() functions take the arguments of the condition to evaluate, the action if the condition is true and the action when condition is false.
## [1] "lessOrEqual" "lessOrEqual" "lessOrEqual" "more" "more"
## [6] "more" "more" "more" "more" "more"
We can use multiple nested ifelse functions to be apply more complex logical to vectors.
## [1] "less" "less" "same" "more" "more" "more" "more" "more" "more" "more"
The two main generic methods of looping in R are while and for
while - while loops repeat the execution of code while a condition evaluates as true.
for - for loops repeat the execution of code for a range of specified values.
While loops are most useful if you know the condition will be satisified but are not sure when (i.e. Looking for a point when a number first occurs in a list).
## x is 1
## x is 2
## Finally x is not less than 3
For loops allow the user to cycle through a range of values applying an operation for every value.
Here we cycle through a numeric vector and print out its value.
## 1 2 3 4 5
Similarly we can cycle through other vector types (or lists).
## A B C D E
We may wish to keep track of the position in x we are evaluating to retrieve the same index in other variables. A common practice is to loop though all possible index positions of x using the expression 1:length(x).
## [1] 1 2 3
## Ikzf1 has an RPKM of 10.4
## Myc has an RPKM of 4.3
## Igll1 has an RPKM of 6.5
Loops can be combined with conditional statements to allow for complex control of their execution over R objects.
x <- 1:13
for(i in 1:13){
if(i > 10){
message("Number ",i," is greater than 10")
}else if(i == 10){
message("Number ",i," is 10")
}else{
message("Number ",i," is less than 10")
}
}
## Number 1 is less than 10
## Number 2 is less than 10
## Number 3 is less than 10
## Number 4 is less than 10
## Number 5 is less than 10
## Number 6 is less than 10
## Number 7 is less than 10
## Number 8 is less than 10
## Number 9 is less than 10
## Number 10 is 10
## Number 11 is greater than 10
## Number 12 is greater than 10
## Number 13 is greater than 10
We can use conditionals to exit a loop if a condition is satisfied, just like a while loop.
x <- 1:13
for(i in 1:13){
if(i < 10){
message("Number ",i," is less than 10")
}else if(i == 10){
message("Number ",i," is 10")
break
}else{
message("Number ",i," is greater than 10")
}
}
## Number 1 is less than 10
## Number 2 is less than 10
## Number 3 is less than 10
## Number 4 is less than 10
## Number 5 is less than 10
## Number 6 is less than 10
## Number 7 is less than 10
## Number 8 is less than 10
## Number 9 is less than 10
## Number 10 is 10
There are functions which allow you to loop over a data type and apply a function to the subsection of that data.
apply - Apply function to rows or columns of a matrix/data frame and return results as a vector,matrix or list.
lapply - Apply function to every element of a vector or list and return results as a list.
sapply - Apply function to every element of a vector or list and return results as a vector,matrix or list.
The apply() function applys a function to the rows or columns of a matrix. The arguments FUN specifies the function to apply and MARGIN whether to apply the functions by rows/columns or both.
apply(DATA,MARGIN,FUN,...)
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
Get the mean of rows
## [1] 1.5 3.5
Get the mean of columns
## [1] 2 3
Additional arguments to be used by the function in the apply loop can be specified after the function argument.
Arguments may be ordered as if passed to function directly. For paste() function however this isn’t possible.
## [1] "1;2" "3;4"
Similar to apply, lapply applies a function to every element of a vector or list.
lapply returns a list object containing the results of evaluating the function.
## [[1]]
## [1] 1
##
## [[2]]
## [1] 2
As with apply() additional arguments can be supplied after the function name argument.
## [[1]]
## [1] 1
##
## [[2]]
## [1] 1
##
## [[3]]
## [1] 2
sapply (smart apply) acts as lapply but attempts to return the results as the most appropriate data type.
Here sapply returns a vector where lapply would return lists.
## [1] 1 2 3 4 5
## [1] 1 2 3 4 5
In this example lapply returns a list of vectors from the quantile function.
## $row1
## [1] 1 2 3 4 5
##
## $row2
## [1] 6 7 8 9 10
##
## $row3
## [1] 11 12 13 14 15
## $row1
## 0% 25% 50% 75% 100%
## 1 2 3 4 5
##
## $row2
## 0% 25% 50% 75% 100%
## 6 7 8 9 10
##
## $row3
## 0% 25% 50% 75% 100%
## 11 12 13 14 15
Here is an example of sapply parsing a result from the quantile function in a smart way.
When a function always returns a vector of the same length, sapply will create a matrix with elements by column.
## row1 row2 row3
## 0% 1 6 11
## 25% 2 7 12
## 50% 3 8 13
## 75% 4 9 14
## 100% 5 10 15
When sapply cannot parse the result to a vector or matrix, a list will be returned.
exampleList <- list(df=data.frame(sample=paste0("patient",1:2), data=c(1,12)),
vec=c(1,3,4,5))
sapply(exampleList, summary)
## $df
## sample data
## Length:2 Min. : 1.00
## Class :character 1st Qu.: 3.75
## Mode :character Median : 6.50
## Mean : 6.50
## 3rd Qu.: 9.25
## Max. :12.00
##
## $vec
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.00 2.50 3.50 3.25 4.25 5.00
Exercise on loops and conditional branching can be found here
Answers can be found here here
You can create your own functions easily in R using the
<- assignment and function
statement.
We put all our input arguments in () and all code to be
executed in {}.
Anything to be received back from the function is indicated by the
return statement.
In this case we can recapitulate the square function.
First lets create a function that can sum two numbers. We can call the function myFirstFunction and set our arguments as num1 and num2.
myFirstFunction <- function(num1,num2){
sumNum <- num1+num2
return(sumNum)
}
myResult <- myFirstFunction(num1=2,num2=3)
myResult
## [1] 5
We can only return 1 object at a time from function. Here we create the multiple of our numbers and try and return alongside our sum.
myFirstFunction <- function(num1,num2){
sumNum <- num1+num2
multipleNum <- num1*num2
return(sumNum,multipleNum)
}
myResult <- myFirstFunction(num1=2,num2=3)
## Error in return(sumNum, multipleNum): multi-argument returns are not permitted
A simple solution is to pass back an object that contains both results. Here we create a named vector to return our multiple and sum.
myFirstFunction <- function(num1,num2){
sumNum <- num1+num2
multipleNum <- num1*num2
VectorOfResults <- c(sumNum,multipleNum)
names(VectorOfResults) <- c("multiple","sum")
return(VectorOfResults)
}
myResult <- myFirstFunction(num1=2,num2=3)
myResult
## multiple sum
## 5 6
Lists are very useful here as they can contain different object classes in their elements and so we can return mixed data types from our functions using this. Below we return a list with elements of the input arguments as a vector and the result as a data.frame.
myFirstFunction <- function(num1,num2){
sumNum <- num1+num2
multipleNum <- num1*num2
InputNumbers <- c(FirstNum=num1,SecondNum=num2)
DF <- data.frame(Sum=sumNum,Multiple=multipleNum)
listToReturn <- list(Input=InputNumbers,Result=DF)
return(listToReturn)
}
myResult <- myFirstFunction(num1=2,num2=3)
myResult
## $Input
## FirstNum SecondNum
## 2 3
##
## $Result
## Sum Multiple
## 1 5 6
In a function containing a return statement, the code up until the return statement is evaluated and anything after the return statement is not evaluated.
myFirstFunction <- function(num1,num2){
sumNum <- num1+num2
multipleNum <- num1*num2
InputNumbers <- c(FirstNum=num1,SecondNum=num2)
DF <- data.frame(Sum=sumNum,Multiple=multipleNum)
listToReturn <- list(Input=InputNumbers,Result=DF)
message("Before return")
return(listToReturn)
message("After return")
}
myResult <- myFirstFunction(num1=2,num2=3)
## Before return
## $Input
## FirstNum SecondNum
## 2 3
##
## $Result
## Sum Multiple
## 1 5 6
If a function does not contain a return statement, the result of the last line in function is returned.
myFirstFunction <- function(num1,num2){
sumNum <- num1+num2
multipleNum <- num1*num2
InputNumbers <- c(FirstNum=num1,SecondNum=num2)
DF <- data.frame(Sum=sumNum,Multiple=multipleNum)
listToReturn <- list(Input=InputNumbers,Result=DF)
listToReturn
}
myResult <- myFirstFunction(num1=2,num2=3)
myResult
## $Input
## FirstNum SecondNum
## 2 3
##
## $Result
## Sum Multiple
## 1 5 6
Variables defined in the arguments or within the function exist only within the environment of the function.
myFirstFunction <- function(num1,num2){
sumNum <- num1+num2
return(sumNum)
}
myResult <- myFirstFunction(num1=2,num2=3)
myResult
## [1] 5
## Error in eval(expr, envir, enclos): object 'sumNum' not found
Variables defined in the global environment outside the function are available within the function.
my3rdNumber <- 4
myFirstFunction <- function(num1,num2){
sumNum <- num1+num2+my3rdNumber
return(sumNum)
}
myResult <- myFirstFunction(num1=2,num2=3)
myResult
## [1] 9
Changes to variables defined in the global environment from within the function will not be reflected in the global environment.
my3rdNumber <- 4
myFirstFunction <- function(num1,num2){
my3rdNumber <- num1+num2+my3rdNumber
return(my3rdNumber)
}
myResult <- myFirstFunction(num1=2,num2=3)
myResult
## [1] 9
## [1] 4
We can use a <<- to assign to the environment outside the function although we will rarely do this
my3rdNumber <- 4
myFirstFunction <- function(num1,num2){
my3rdNumber <<- num1+num2+my3rdNumber
return(my3rdNumber)
}
myResult <- myFirstFunction(num1=2,num2=3)
myResult
## [1] 9
## [1] 9
Functions can have defaults for their arguments which will be used when arguments are not specified.
myFirstFunction <- function(num1=1,num2=3){
my3rdNumber <- num1+num2+my3rdNumber
return(my3rdNumber)
}
myFirstFunction()
## [1] 13
## [1] 16
User defined functions can be as long and complicated as you want, taking in many variables, printing out messages and utilizing other functions within them.
Lets make z-scores:
my_zscore <- function(my_number, my_vector){
my_mean <- mean(my_vector)
message("Mean is ", my_mean)
diff_from_mean <- my_number-my_mean
stdev <- sd(my_vector)
my_z <- diff_from_mean/stdev
return(my_z)
}
A <- rnorm(20)
my_zscore(my_number=A[1], my_vector=A)
## Mean is -0.186024460821081
## [1] 0.8948993
In Rstudio, if we want to see what may be happening within our function we can use debug function with our function of interest’s name. We can stop debugging with the undebug function.
These custom functions can also be utilized with apply.
## [1] 0.89489931 -0.02965885 -1.72706895 -0.18808151 0.84449689 2.15899574
## [7] -0.21524073 -0.07665160 1.70488730 -1.21029848 0.04219758 1.05041236
## [13] -0.86045033 -0.39379250 -0.41399727 0.21903212 0.85581804 -0.85026968
## [19] -1.09915530 -0.70607414
Libraries can be loaded using the library() function with an argument of the name of the library.
You can see what libraries are available in the Packages panel or by the library() function with no arguments supplied.
Once we have got our functions together and know how we want to analyse our data, we can save our analysis as a script. By convention R scripts typically end in .r or .R
To save a file in RStudio.
-> File -> Save as
To open a previous R script
->File -> Open File..
To save all the objects (workspace) with extension .RData
->Session -> Save workspace as
R scripts allow us to save and reuse custom functions we have written. To run the code from an R script we can use the source() function with the name of the R script as the argument.
The file dayOfWeek.r in the “scripts” directory contains a simple R script to tell you what day it is.
#Contents of dayOfWeek.r
dayOfWeek <- function(){
return(gsub(" .*","",date()))
}
## [1] "Wed"
R scripts can be run non-interactively from the command line with the Rscript command, usually with the option –vanilla to avoid saving or restoring workspaces. All messages/warnings/errors will be output to the console.
Rscript --vanilla myscript.r
An alternative to Rscript is R CMD BATCH. Here all messages/warnings/errors are directed to a file and the processing time appended.
R CMD BATCH myscript.r
To provide arguments to an R script at the command line we must add commandArgs() function to parse command line arguments.
'10'
10
Since vectors can only be one type, all command line arguments are strings and must be converted to numeric if needed with as.numeric().
Exercise on functions can be found here
Answers can be found here here