R Basics - bcb420-2023/Maryam_Hasanzadehkiabi GitHub Wiki

Notes on R Basics Unit

Objectives:

  • installing R, RStudio, Docker
  • Getting started with GitHub
  • introduction to R commands

Time estimated: 3 h; taken 12 h; date started: 2023-01-11; date completed: 2023-03-29


Chapter 1 About

  • download R -> make sure its right one for your system
  • download RStudio -> make sure its right one for your system
  • or just download Docker
  • using Docker helps with the ever changing and updates
  • using Docker container allows you to reproduce your results in the exact environment anywhere (with the same packages, libraries, etc.) without having to do the manual work of checking to see if everything is the same set-up
  • using Docker allows you to test different versions and never losing the version that was working before
  • use the corresponding Docker image for this course to run Assignments to make sure you get no errors when trying to get HTML notebook
  • you can run multiple Docker containers at the same time
  • a container is from an image of that instance that you create (you make images by giving instructions of what to include in it, an image is the set of instructions - the reciepe)
  • you can make images from previous images and add to it
  • you can make multiple containers using same image
  • Docker image - is reciepe + ingredients
  • Docker container - is different dishes/food you make using the reciepe and ingredients
  • Docker volumes - a filesystem that will keep everything in a container even after you delete the container, it "maps a drive on the host system to a drive on the container" - 1.8.3 Docker volumes from R Basics Unit
  • Docker set-up on MacOS:
  • open terminal
  • open Docker
  • go to the folder you linked to git and the place you want keep all the stuff
  • run: docker run -e PASSWORD=changeit --rm
    -v "$(pwd)":/home/rstudio/projects -p 8787:8787
    risserlin/bcb420-base-image:winter2023
  • go on web browser and search localhost8787, enter username: rstudio, password: changeit
  • to specifiy where you save a copy on your local machine add after /projects line
  • created a rmd notebook on the localhost RStudio and found in my local machine too
  • packages:
  • need to install package
  • to use the package and its functions you can:
  1. packagename::function/library(x)
  2. or you can load the libraries seperately: library(x) **- do task 1.12.1 exercise ** -> completed!
  • to help with finding what packages to use, use: if (! requireNamespace("sos", quietly=TRUE)) { install.packages("sos") } library(help = sos) # basic information browseVignettes("sos") # available vignettes

sos::findFn("moving average")

  • when installing packages use: if (! requireNamespace("seqinr", quietly = TRUE)) { install.packages("seqinr") }

  • exercise 1.12.1 solutions

after running the following chunk the result is:

seqinr::words(3, c("A", "B", "C", "U"))

[1] "AAA" "AAB" "AAC" "AAU" "ABA" "ABB" [7] "ABC" "ABU" "ACA" "ACB" "ACC" "ACU" [13] "AUA" "AUB" "AUC" "AUU" "BAA" "BAB" [19] "BAC" "BAU" "BBA" "BBB" "BBC" "BBU" [25] "BCA" "BCB" "BCC" "BCU" "BUA" "BUB" [31] "BUC" "BUU" "CAA" "CAB" "CAC" "CAU" [37] "CBA" "CBB" "CBC" "CBU" "CCA" "CCB" [43] "CCC" "CCU" "CUA" "CUB" "CUC" "CUU" [49] "UAA" "UAB" "UAC" "UAU" "UBA" "UBB" [55] "UBC" "UBU" "UCA" "UCB" "UCC" "UCU" [61] "UUA" "UUB" "UUC" "UUU"

  • completed tasks 1.14 to 1.23:
  • checked course folder i am working on a mac and my set up is /Users/Maryam/Documents/BCB420
  • i linked my RStudio with Git
  • when in RStudio web browser i set up working directory to mirror my desktop folder

Chapter 2

  • the console and writing are different
  • in the console you can execute code typed directly by entering you dont have to run a chunk or script
  • RNotebooks:
  • better than scripts
  • use this to create different formats of your work ( when you run and save it saves as .rmd and .HTML/.PDF/.docx
  • better and faster of running and getting your chunk runs results top to bottom by just running once
  • you can access different version of your notebook to check progress and previous work

Chapter 3

  • if need help with R use the help in R itself - R Help
  • if you need help with different functions use help("function")
  • you can also call help by typing: ?r"function"
  • if you need help with Errors try searching on google, stackoverflow, crossvalidated
  • you can also make a reproducible dataset by using dput() function and post that in the class discussion group

Chapter 4

  • math operators work the same
  • DO TASK 12 4.4 ********* -> completed!
  • variable <- aa
  • dont use variable names that are used by R
  • dont use variable names such as: col, row, head, hist use a more unique name instead

Chapter 5

  • scalar: single value ex. a <- 22 length(a) is one a[1] you get 22 a[2] you get NA this is actually a vector of only one element

  • vectors: ordered sequence, same data type (all numbers, all strings, etc)

  • matrices: vectors with multiple data types

  • data frames: like spreadsheets

  • lists: a group of items

  • Booleans: TRUE and FALSE have mode logical

  • integers, floats and complex numbers have mode numeric

  • strings have mode character

  • function mode() gives the mode of object

  • function typeof() gives the type of the object

  • function class() gives the class the object belongs to

  • DO TASK 13 ****** -> Completed!

  • function c() makes any element into a vector ex. c(1,2,3) gives [1] 1 2 3 ex. b <- c(22,33,44) gives a vector [1] 22 33 44 ex. length(b) gives [1] 3 ex. c <- 10 gives [1] 10 ex. (d <- c(11,12,13,14,15)) gives [1] 11 12 13 14 15

  • if not all elements of a vector are same type it silently changes them to character

  • head() gives the first element

  • length() gives the last element index

  • tail() gives the last element ex. d[1] gives [1] 11 ex. head(d, 1) gives [1] 11 ex. d[length(d)] gives 15 ex. 1:5 gives [1] 1 2 3 4 5 ex. e[1:4] gives [1] 1 1 3 5 ex. f[4:1] gives [1] 5 3 1 1

  • function seq() gives sequences ex. seq(from=3, to=9, b=3) it gives [1] 3 6 9

ex. seq(3, 9, 3) it gives [1] 3 6 9

ex. minevec[seq(2, 6,2)] gives [1] 1 5 13

ex. (n <- -(1:4)) gives [1] -1 -2 -3 -4

  • you subset by name, boolean, etc

  • DO TASK 14 ************** -> Completed!

  • to remove first element of vector: ( alpha <- minevect[-1])

  • to remove the last element of vector: (beta <- minevect[1:(length(minevect)-1)])

  • dimensional and matrices: ex. (alpha <-1:12) gives [1] 1 1 2 3 4 5 6 7 8 9 10 11 12 ex. dim(alpha) <- c(2,6) alpha gives divides the elements into 6 columns and 2 rows

ex. dim(alpha) <- c(4,3) alpha gives the elements divided into 3 columns and 4 rows

ex. dim(alpha) <- c(2,2,3) alpha gives the elements divided into 3 groups of 2 columns and 2 rows

  • for example: dim(c) gives dimension of vector dim(c)[4] gives the size of 4th dimension of c
  • 2D matrix:
  • function nrow() and ncol() will give number of rows and columns
  • dim(a)[1] is equal to nrow(a)
  • you can combine vectors by rows and columns using function rbind() , cbind()
  • DO TASK 15 ---> completed!

Chapter 6

  • data frame is how you read a file that you input
  • to read a data frame follow this:

(name of file you want to read <- read.table(file.path("where it is"), sep="\t", header=TRUE, stringsAsFactors = FALSE) )

  • stringsAsFactors:

when stringsAsFactors = TRUE --> all strings will be turned into factors

  • to make column 1 as rownames:

rownames(your file name) <- your file name[ ,1] now(your file name)

ncol(your file name)

objectInfo(your file name)

  • to make a entire row into a variable:

x <- your file name[2, ] objectInfo(x)

  • to get information of one row only: your file name["first cell of that row", ]

  • to get info for one column only: your file name[ , 2]

or

your file name[ , "first cell of that column]

  • to remove one row: your file name <- your file name[-2, ] objectInfo(your file name)

  • to add a row at the end of file: your file name <- bind(your file name, x) objectInfo(your file name)

  • to add a new row: you file are <- bind(your file name, data.frame(Name = "namex", Size= #, Marker = "Amp", Ori = "pMB1", Sites = "... ", stringsAsFactors = FALSE)) objectInfo(your file name)

  • do task 17 ** -> Completed!

Chapter 7

  • do task 18 * -> completed!
  • to create a list use function list()

  • use $ to get to a cell thats defined already in the list

  • do task 19 * -> completed!
  • in list you can't all the data directly you have to go over all items using function apply()

example: yourlist <- list() yourlist"mmmm" <- mmmm apply(yourlist, function(x) { return(x$ori) })

Chapter 8

  • subsetting and filtering
  • Do task 20 --> Completed!
  • single item thats in the brakets
  • $ single item examples: mydata[1,0] mydata[2,0]

to get more than one row: mydata[c(1,2),] or mydata[c(3,1),] or mydata[c(1,2,1,2,1,2),]

  • to extract only columns mydata[, 2]

  • to select rows and columns by their name ex. mydata[, "thename"] ex. mydata$thename

  • use function order() to sort values

  • use function grep() to get vectors

  • you can replace objects" ex. (y <-mysample(1:10)) y[4] <- 99 y

(y <- y[order(y)])

  • Do task 21 -> Completed!

Chapter 9

  • control structures (if, else, for, while)
  • from chpter

References