## Getting help help("mean") ## or ?mean # for a particular package vignette("extending-ggplot2") vignette("phyloseq-basics") ## Calculator 2+2 ## Assigning a value to a variable a <- 2*4 ## or a = 2*4 (the # sign signals a comment) a a/2 a ## Changing the value of a variable a <- 4 a b <- a; b <- 8 a; b ## Removing a variable a rm(a) a ## Elementary modes in R ## ----numeric------------------------------------------------------------- x <- 1 class(x) ## ----character------------------------------------------------------------- x <- "hello" class(x) ## ----logical------------------------------------------------------------- x <- TRUE ## or FALSE class(x) ## length function a <- 2 length(a) ## Special values a <- NA; length(a); is.na(a) x <- NULL; length(x); is.null(x) ############# ## VECTORS ## ############# ## ----c-command----------------------------------------------------------- x <- c(2, 4, 8, 9, 0) x ## prints x ## ----subsetting-vector-1------------------------------------------------- x[1] ## first element ## ----subsetting-vector-2------------------------------------------------- x[c(3, 5)] ## third and fifth elements ## ----coercion-vector-1--------------------------------------------------- c(3.4, 2, TRUE) ## ----coercion-vector-2--------------------------------------------------- c(3.4, "MaIAGE", TRUE) ## ----named-vector-------------------------------------------------------- x <- c("first" = 1, "second" = 4, "third" = 9) x ## ----named-vector-1------------------------------------------------------ x[1] ## ----named-vector-2------------------------------------------------------ x["first"] ## ----named-vector-3------------------------------------------------------ x <- c(1, 4, 9) x names(x) <- c("first", "second", "third") x ## Try to guess the result x <- c("O", "G", "F", "S", "R") x[c(3, 5, 1, 2, 4)] ## ----logical-indexing-1-------------------------------------------------- x <- 1:6 x index <- c(TRUE, FALSE, TRUE, TRUE, FALSE, FALSE) x[index] ## = x[c(1, 3, 4)] ## ----logical-indexing-2-------------------------------------------------- index <- c(TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE) x[index] ## = x[c(1, 3, 4, 7)] but x[7] does not exist ## Exercice rank <- c("Order", "Kingdom", "Genus", "Class", "Family", "Species", "Phylum") # Create a new_rank vector based on rank in the correct order ############ ## MATRIX ## ############ ## ----matrices------------------------------------------------------------ x <- matrix(1:18, nrow = 3, ncol = 6) ## compare to x <- matrix(1:18, nrow = 3, ncol = 6, byrow = TRUE) x x[2, 4] ## element in 2nd row, 4th column ## ----indexing-matrix-1--------------------------------------------------- x[, 2] ## 2nd column ## ----indexing-matrix-2--------------------------------------------------- x[2, ] ## 2nd row ## Try to guess the result x <- as.matrix(read.csv("data/introR/matrix.tsv", row.names=1, sep="\t")) x x[ , 3] x["otu_2",] x[c(1, 2), ] x[c(1, 3), c(2, 3)] ## How to access to the count of sample2 for otu_3 ? ############ ## FACTOR ## ############ ## ----factor-example------------------------------------------------------ x <- factor(c("a", "a", "b", "a", "c")) class(x) ## ----levels-------------------------------------------------------------- levels(x) ## ----relevel------------------------------------------------------------- y <- factor(x, levels = c("b", "a", "c")) levels(y) ## Try to guess the result x <- c("a" = 1, "b" = 2, "c" = 3) y <- c("a", "b", "c") x[y] z1 <- factor(y, levels = c("a", "b", "c")) z2 <- factor(y, levels = c("b", "a", "c")) ## First version: with z1 z1 x[z1] ## Second version: with z2 z2 x[z2] ################ ## DATA FRAME ## ################ ## ----data.frame, echo = -1----------------------------------------------- x <- data.frame(number = c(1:4), group = factor(c("A", "A", "B", "B")), desc = c("riri", "fifi", "lulu", "picsou")) ## Characteristics of x x class(x) class(x[,1]) class(x[,2]) x[2, "desc"] ## or x[2, 3] dim(x); nrow(x); ncol(x) x$group ## Exercice setup x <- data.frame(ID = 1:10, group = rep(c("A", "B"), each = 5), value = rnorm(10)) x ## Try to guess the result ii <- 1:5 df <- x[ii, c("ID", "value")] df df[ , 2] class(df[ , 2]) df[2, ] class(df[2, ]) ############################# ## BUILDING LOGICAL FILTER ## ############################# x <- 1:5 x z <- (x < 3); z ## the first command returns a logical vector z <- (x < 4) & (x > 1); z ## logical AND z <- (x < 2) | (x > 4); z ## logical OR !z ## logical not which(z) z <- (x < 4) x[z] x[x < 4] sum(z) ############# ## GGPLOT2 ## ############# ## ----load-ggplot, cache = FALSE------------------------------------------ library(ggplot2) ## ----diamonds------------------------------------------------------------ data(diamonds) ## import datasets class(diamonds) head(diamonds) ## first line of data.frame ## ----ggplot-first-example------------------------------------------------ ## set base plot, x coordinate is carat, y is price p <- ggplot(diamonds, mapping = aes(x = carat, y = price)) ## Add a layer to represent data as point p1 <- p + geom_point() plot(p1) ## ----ggplot-color-example------------------------------------------------ ## set base plot, x coordinate is carat, y is price colored by cut p <- ggplot(diamonds, mapping = aes(x = carat, y = price, color=cut)) ## Add a layer to represent data as point p2 <- p + geom_point() plot(p2) ## OR p <- ggplot(diamonds, mapping = aes(x = carat, y = price)) ## Add a layer to represent data as point p2 <- p + geom_point(aes(color=cut)) plot(p2) ## ----ggplot-boxplot-example------------------------------------------------ ## Add a layer to represent data as boxplot p <- ggplot(diamonds, mapping = aes(x = cut, y = price)) p3 <- p + geom_boxplot() plot(p3) # colored by clarity p4 <- ggplot(diamonds, mapping = aes(x = cut, y = price, color=clarity)) + geom_boxplot() plot(p4) ########## FACETTING ## ----facet_wrap, fig.width = 10, fig.height = 4-------------------------- ## facet along cut p5 <- p2+ facet_wrap(~ cut) plot(p5) ## ----facet_grid, fig.width = 10, fig.height = 2.5------------------------ ## facet along cut, only points from a given cut appear in a facet p6 <- p2 + facet_grid(~ cut) plot(p6) ## ----facet_grid-2-factors, fig.width = 10, fig.height = 5, size = "tiny"---- ## facet along clarity (rows) * cut (column) p7 <- p2 + facet_grid(clarity ~ cut) plot(p7) ## On boxplot p8 <- p4 + facet_wrap(~cut) plot(p8) ## free x p9 <- p4 + facet_wrap(~cut, scales="free_x") plot(p9) ## ----palette------------------------------------------------------------- palette <- c("black", "red", "blue", "magenta", "gray") names(palette) <- c("Fair", "Good", "Very Good", "Premium", "Ideal") ## ----second-example-1, size = "tiny", fig.height = 4.5------------------- ## Manual color scale p6.1 <- p6 + scale_color_manual(values = palette) plot(p6.1) ## ----second-example-2, size = "tiny", fig.height = 4.5------------------- ## Use built-in color palette p7.1 <- p7 + scale_color_brewer() plot(p7.1) ## Add title p10 <- p9 + ggtitle("Diamond prices as a function of clarity") + xlab("Diamond clarity") + ylab("Diamond price") plot(p10) ################ ## GO FURTHER ## ################ ################# ## CONVERSIONS ## ################# ## Examples as.integer("5") as.logical(0.0) as.numeric(TRUE) as.character(TRUE) as.numeric("5.56") as.logical(2) as.integer("INRA") ## Try to guess the result: integer as.integer(2/3) as.integer(5.67) as.integer(FALSE) as.integer(TRUE) as.integer("5.67") as.integer("MaIAGE") ## Try to guess the result: numeric as.numeric(2/3) as.numeric(5.67) as.numeric(FALSE) as.numeric(TRUE) as.numeric("5.67") as.numeric("MaIAGE") ## Try to guess the result: character as.character(2/3) as.character(5.67) as.character(FALSE) as.character(TRUE) as.character(5) as.character(5+7) ## Try to guess the result: logical as.logical(2/3) as.logical(0) as.logical("45") as.logical("MaIAGE") ## Guess the result : TRUE + TRUE + FALSE * TRUE + TRUE * TRUE ## ----converting-vector-1------------------------------------------------- as.numeric(x) ## 1="a", 2="b" as.character(x) ## ----converting-vector-2------------------------------------------------- as.integer(y) ## 1="b", 2="a" as.character(y)