####################### ## Installation of R ## ####################### ## R: http://cran.at.r-project.org ## RStudio: http://www.rstudio.org/ ## Installation of CRAN Packages ## Online # install.packages(c("pkg1", "pkg2")) ## Local # install.packages("pkg.zip", repos=NULL) ## Installation of BioConductor Packages # source("http://www.bioconductor.org/biocLite.R") # biocLite() # biocLite(c("pkg1", "pkg2")) #################### ## Getting Around ## #################### ## Create an object with the assignment operator ’<-’ (or ’=’) object <- ... ## List objects in current R session ls() ## Return content of current working directory dir() ## Return path of current working directory getwd() ## Change current working directory # setwd("/Users/tgirke/Desktop") #################### ## Basic R Syntax ## #################### ## General R command syntax object <- function(arguments) object <- object[arguments] ## Execute an R script source("my script.R") ## Execute an R script from command-line $ R CMD BATCH myscript.R $ R --slave < myscript.R $ ./myscript.R ## Finding help ?function ## Load a library library("grid") ## Summary of all functions within a library library(help="grid") ## Load library manual (PDF file) vignette("grid") ################ ## Data Types ## ################ ## Numeric data: 1, 2, 3 x <- c(1, 2, 3) x is.numeric(x) as.character(x) ## Character data: "a", "b", "c" x <- c("1", "2", "3") x is.character(x) as.numeric(x) ## Complex data: 1, b, 3 c(1, "b", 3) ## Logical data: TRUE, FALSE, TRUE x <- 1:10 < 5 x > !x ## Return indices for the ’TRUEs’ in logical vector which(x) ################## ## Data Objects ## ################## ## Vectors (1D) myVec <- 1:10 names(myVec) <- letters[1:10] myVec[1:5] myVec[c(2,4,6,8)] myVec[c("b", "d", "f")] ## Factors (1D): vectors with grouping information factor(c("dog", "cat", "mouse", "dog", "dog", "cat")) ## Matrices (2D), Data Frames (2D) and Arrays (≥2D) myMA <- matrix(1:30, 3, 10, byrow = T) myDF <- data.frame(Col1=1:10, Col2=10:1) myDF[1:4, ] myDF[ ,c("Col2", "Col1", "Col1")] ## Lists: containers for any object type myL <- list(name="Fred", wife="Mary", no.children=3, child.ages=c(4,7,9)) myL[[4]][1:2] ## Functions: piece of code myfct <- function(arg1, arg2, ...) { function body } ############################## ## General Subsetting Rules ## ############################## ## Subsetting by indices myVec <- 1:26 names(myVec) <- LETTERS myVec[1:4] ## Subsetting by same length logical vectors myLog <- myVec > 10 myVec[myLog] ## Subsetting by field names myVec[c("B", "K", "M")] ## Special case iris$Species ###################################### ## Basic Operators and Calculations ## ###################################### ## Comparison operators: ==, ! =, <, >, <=, >= ## Example: 1==1 ## Logical operators: AND: &, OR: |, NOT: ! ## Example: x <- 1:10 y <- 10:1 x > y & x > 5 ## Calculations: ## Example: x + y sum(x) mean(x) sd(x) sqrt(x) apply(iris[,1:3], 1, mean) ####################################### ## Reading and Writing External Data ## ####################################### ## Import Data into R myframe <- read.delim("http://faculty.ucr.edu/~tgirke/Documents/R_BioCond/Samples/MolecularWeight_tair7.xls", sep="\t") myframe[1:4,] ## Write Data from R to File write.table(myframe, file="myfile.xls", sep="\t", quote=F) ############################ ## Some Great R Functions ## ############################ ## The unique() function to make vector entries unique unique(iris$Species) length(unique(iris$Species)) ## The table() function counts the occurrences of entries table(iris$Species) ## The aggregate() function computes statistics of data aggregates aggregate(iris[,1:4], by=list(iris$Species), FUN=mean, na.rm=T) ## The %in% function returns the intersect between two vectors month.name[month.name %in% c("May", "July")] ## The merge() function joins data frames based on a common key column merge(frame1, frame2, by.x=1, by.y=1, all = TRUE) ## The apply() function simplifies the coding of iterative tasks (loops). ## Creates a sample data set. myMA <- matrix(rnorm(100000), 10000, 10, dimnames=list(1:10000, paste("C", 1:10, sep=""))) myMA[1:4,] ## Computes the mean for each row in myMA. apply(myMA, 1, mean)[1:4] ## Simplified computation on column aggregates source("http://faculty.ucr.edu/~tgirke/Documents/R_BioCond/My_R_Scripts/colAg.R") # Imports the colAg() function. colAg(myMA=myMA, group=c(1,1,1,2,2,2,3,3,4,4), myfct=mean)[1:4,] ############################ ## Some Graphics Commands ## ############################ ## Dot plots plot(1:10) plot(iris[,1:4]) ## Barplot barplot(1:10) ## Help with plots ?plot ?par