rm(list=ls()) #Fresh start clear all the visible objects from workspace ######################################################### #: indicates to R that what follows is a comment line and R ignores it # Slide 8 #install CRAN package: #install.packages("caret", dependencies = T) #install.packages("tidyverse", dependencies = T) # or #install.packages(c("tidyverse", "ggfortify", "randomForest", "plyr", "rfUtilities", "caret", "ROCR"), dependencies = T) ########### #install Bioconductor package: #if (!requireNamespace("BiocManager", quietly = T)) #install.packages("BiocManager") #BiocManager::install() # or #To install specific packages: #BiocManager::install(c("GenomicFeatures", "AnnotationDbi")) ######################################################### # Slide 9 #install github packages: #install.packages("devtools") #install_github("benjjneb/dada2") ######################################################### # Slide 10 #Getting help on functions and packages: library(readr) # List all functions in the package readr: ls("package:readr") # Get help page for read_table function: ?read_table #or help("read_table") # Search the word "read" in help pages: help.search("read") #or ??read #Getting help on operators: ?"+" ?">" ######################################################### # Slide 13 #The class() function in R helps us determine the type of data we have: class(1) #[1] "numeric" class("a") #[1] "character" class(T) #[1] "logical" class("T") #[1] "character" class(1.4) #[1] "numeric" class(1L) #[1] "integer" ######################################################### # Slide 15 myVec<-c(1, 2, 3, 4, 5) print(myVec) #[1] 1 2 3 4 5 myVec<-c("a", "b", "c", "d") print(myVec) #[1] "a" "b" "c" "d" myVec<-letters[1:4] # or myVec<-toupper(letters[1:4]) print(myVec) #[1] "a" "b" "c" "d" # or [1] "A" "B" "C" "D" #You can test if the structure is a vector or not using the is.vector() function: is.vector(myVec) #[1] TRUE ######################################################### # Slide 16 #myMat<-matrix(data, num. rows, num. columns) myMat<-matrix(c(1,2,3,11,12,13), 2, 3) ######################################################### # Slide 17 print(myMat) is.matrix(myMat) ######################################################### # Slide 18 myDF<-data.frame(chr=c(1,2,3), strand=c("-", "+", "+"), start=c(100,2000,10000), end= c(550,3000, 11230)) ######################################################### # Slide 19 print(myDF) is.data.frame(myDF) ######################################################### # Slide 20 myList<-list(vec=myVec, mat=myMat, df=myDF, age=25, correct=F) is.list(myList) ######################################################### # Slide 21 print(myList) ######################################################### # Slide 23 #assume we want to read myFile.txt, which is a tab delimited text file: #read.table("myFile.txt", sep="\t", header=T) #or #read.delim("myFile.txt") #assume we want to read myFile.csv, which is a text file with comma separated values: #read.csv("myFile.csv") #write myDF to tab delimited text file named myDF.txt: write.table(myDF, file ="myDF.txt", sep="\t", row.names=T, col.names=T) #write myDF to comma separated values text file named myDF.csv: write.csv(myDF, file ="myDF.csv") ######################################################### # Slide 24 #save myDF to R dat binary file named myDF.dat: save(myDF, file="myDF.dat") #Note: you can save multiple objects with one command: save(myDF, myVec.f, myList, file="myDF.dat") #read myDF.dat R dat binary file: load("myDF.dat") #write myDF to a single R object file: saveRDS(myDF, file="myDF.rds") #read single R object file: myData<-readRDS("myDF.rds") ######################################################### # Slide 26 #if and if-else statements: x <- 1 if(x!=1){ print(sqrt(x)) } else { print("I dont want to calculate the square root of 1") } ######################################################### # Slide 27 #for loop x <- c(1,2,3,4,5,6,7,8,9,10) x_sum <- 0 for(i in 1:length(x)){ x_sum <- x_sum + x[i] } print(x_sum) #[1] 55 ######################################################### # Slide 28 #x_sum function x_sum <- function(y){ my_sum <- 0 for(i in 1:length(y)){ my_sum <- my_sum + y[i] } return(my_sum) } x <- c(1,2,3,4,5,6,7,8,9,10) x_sum(x) #[1] 55 ######################################################### # Slide 29 # sum: a pre-defined function in R provided by “base” package: x <- c(1,2,3,4,5,6,7,8,9,10) sum(x) #[1] 55 #########################################################