Skip to content
Course Notes: Introduction to R
  • AI Chat
  • Code
  • Report
  • Course Notes

    Use this workspace to take notes, store code snippets, or build your own interactive cheatsheet! For courses that use data, the datasets will be available in the datasets folder.

    Vectors

    vectors are one-dimension arrays that can hold numeric data, character data, or logical data.

    Add your notes here

    # Add your code snippets here
    numeric_vector <- c(1, 2, 3)
    character_vector <- c("a", "b", "c")
    Vector_slice <- Vector[Vector>0]
    my_matrix<-matrix(1:9,byrow = TRUE,nrow=3)
    rownames(my_matrix) <- row_names_vector
    colnames(my_matrix) <- col_names_vector

    big_matrix <- cbind(matrix1, matrix2, vector1 ...)

    my_matrix[1,2] my_matrix[1:3,2:4] my_matrix[,1] selects all elements of the first column. my_matrix[1,] selects all elements of the first row.

    survey_vector <- c("M", "F", "F", "M", "M")
    factor_survey_vector <- factor(survey_vector)
    levels(factor_survey_vector) <- c("Female", "Male")
    factor_survey_vector
    
    factor(some_vector,
           ordered = TRUE,
           levels = c("lev1", "lev2"))
    

    Data Frames

    # Data Frame
    name <- c("Mercury", "Venus", "Earth", 
              "Mars", "Jupiter", "Saturn", 
              "Uranus", "Neptune")
    type <- c("Terrestrial planet", 
              "Terrestrial planet", 
              "Terrestrial planet", 
              "Terrestrial planet", "Gas giant", 
              "Gas giant", "Gas giant", "Gas giant")
    diameter <- c(0.382, 0.949, 1, 0.532, 
                  11.209, 9.449, 4.007, 3.883)
    rotation <- c(58.64, -243.02, 1, 1.03, 
                  0.41, 0.43, -0.72, 0.67)
    rings <- c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)
    
    # Create a data frame from the vectors
    planets_df <-data.frame(name,type,diameter,rotation,rings)
    planets_df
    str(planets_df)
    
    # Print out diameter of Mercury (row 1, column 3)
    planets_df[1,3]
    
    # Print out data for Mars (entire fourth row)
    planets_df[4,]
    
    # Select first 5 values of diameter column
    planets_df[1:5,"diameter"]
    
    #3 notations
    planets_df[,3]
    planets_df[,"diameter"]
    planets_df$diameter
    
    #subset
    # Select planets with diameter < 1
    subset(planets_df,diameter<1)
    subset(planets_df,rings==1)
    
    #logical expressions
    # If batrips was a data frame
    batrips[batrips$subscription_type =="Subscriber",]
    
    # If batrips was a data frame
    batrips[batrips$start_terminal ==58& batrips$end_terminal !=65]
    
    #note that these expressions are more simple in data.table
    
    #sort
    a <- c(100, 10, 1000)
    order(a)
    [1] 2 1 3
    
    a[order(a)]
    [1]   10  100 1000
    
    
    
    planets_df
         name               type diameter rotation rings
    1 Mercury Terrestrial planet    0.382    58.64 FALSE
    2   Venus Terrestrial planet    0.949  -243.02 FALSE
    3   Earth Terrestrial planet    1.000     1.00 FALSE
    4    Mars Terrestrial planet    0.532     1.03 FALSE
    5 Jupiter          Gas giant   11.209     0.41  TRUE
    6  Saturn          Gas giant    9.449     0.43  TRUE
    7  Uranus          Gas giant    4.007    -0.72  TRUE
    8 Neptune          Gas giant    3.883     0.67  TRUE
    
    # Use order() to create positions
    positions <-  order(planets_df$diameter)
    
    positions
    [1] 1 4 2 3 8 7 6 5
    
    #order() function returns a vector of positions that should be used to sort the rows of the data frame. 
    planets_df[positions,]
         name               type diameter rotation rings
    1 Mercury Terrestrial planet    0.382    58.64 FALSE
    4    Mars Terrestrial planet    0.532     1.03 FALSE
    2   Venus Terrestrial planet    0.949  -243.02 FALSE
    3   Earth Terrestrial planet    1.000     1.00 FALSE
    8 Neptune          Gas giant    3.883     0.67  TRUE
    7  Uranus          Gas giant    4.007    -0.72  TRUE
    6  Saturn          Gas giant    9.449     0.43  TRUE
    5 Jupiter          Gas giant   11.209     0.41  TRUE

    List

    Vectors (one dimensional array): can hold numeric, character or logical values. The elements in a vector all have the same data type.

    Matrices (two dimensional array): can hold numeric, character or logical values. The elements in a matrix all have the same data type.

    Data frames (two-dimensional objects): can hold numeric, character or logical values. Within a column all elements have the same data type, but different columns can be of different data type.

    A list in R is similar to your to-do list at work or school: the different items on that list most likely differ in length, characteristic, and type of activity that has to be done.