Course Notes: Introduction to R
  • AI Chat
  • Code
  • Report
  • Beta
    Spinner

    Course Notes

    Use this workspace to take notes, store code snippets, and build your own interactive cheatsheet!

    # Import any packages you want to use here
    

    Take Notes

    Add notes here about the concepts you've learned and code cells with code you want to keep.

    Add your notes here

    # Add your code snippets here
    

    INTRO to R

    • No need of print function in the console but print() is used in the editor
    • <- is used for assignment
    • R is case senstive
    • there are four data types i.e # integer---whole numbers # nemeric---decimal numbers # characters---string/text # logical------Booleans ie TRUE,FALSE

    VECTORS IN R

    • one-dimension arrays that can hold numeric data, character data, or logical data
    • They are created using the combine function** c(). e.g # numeric_vector <- c(1, 2, 3) # character_vector <- c("a", "b", "c") # boolean_vector <- c(TRUE,FALSE,TRUE) *the c() is also used to combine two or more vectors into one. e.g new_vector <- c(numeric_vector,character_vector). this returns c(1, 2, 3, "a", "b", "c" ) *content of the vector are given name using name() function e.g name(numeric_vector) <- c(A, B, C) where A is for 1, B is for 2, C is for 3. *when two or more vector are summed,the addition is done element wise e.g x_vector <- c(1, 2, 3) y_vector <- c(3, 6, 4) x_vector + y_vector will result to c(1+3, 2+6, 3+4) *the sum() adds the content of a given vector. e.g sum(x_vector) will be 1+2+3 *to select specific elements of the vector we use the square brackets.
    • Between the square brackets, you indicate what elements to select.
    • the first element correspond to index 1 and not 0.
    • to select multiple elements we use sq brackets n combine function [c()].
    • the position of the elements to be selected are indicate within the c() and seperated comma.
    • If the sequence of seperation is continous,only the first and last element position are indicated.
    • the two are seperated with a colon
    • the assign names can also be used to select elements of a vector.
    • logical comparison is possible for vectors.
    • followin operators are used- < less than > greater than <= less than or equal to >= greater than or equal to == equal to each other != not equal to each other e.g c(4, 5, 7, 8) > 5 results to FALSE FALSE TRUE TRUE COMPRASION is done for each element. if we write the original vector aganist a resulting vector of a comparison enclosed in a sq bracket we get a return of elements whose comparison evaluate to true.

    Matrix in R

    • matrix is a collection of elements of the same data type arranged into a fixed number of rows and columns.

    • matrix is constructed using a matrix() function**

    • Syntax matrix(i:n, byrow = TRUE, nrow = j ) where i:n--- the collection of elements to transformed into a matrix byrow--- the matrix is filled by the rows. if set to FALSE the matrix is filled columnwise. nrow---- indicates that the matrix should have j rows.