Exercise 1: Vector

Author

Muhammad Danial Bin Hakasah

Exercise 1

  1. create a numeric “num_var” with the values “42.5”

    num_var<- 42.5 
    num_var
    [1] 42.5
  2. create a character variable “char_var” with the value ” R is fun! ”

    char_var<- " R is fun!" 
    char_var
    [1] " R is fun!"
  3. Print the data type ” char_var”

    char_var
    [1] " R is fun!"
  4. Create a list ” student_info” with the following element

    a. “name” Mohammad Nasir Abdullah

    b. “age” 18

    c. “grades” a numeric vector with values “99, 100, 89”

    student_info<- list(
      name=c("Mohammad_Nasir_Abdullah"), 
      age=c(18),
      grades=c(99,100,89)
      )
    student_info
    $name
    [1] "Mohammad_Nasir_Abdullah"
    
    $age
    [1] 18
    
    $grades
    [1]  99 100  89
  5. Create data frame ” df_student ” with the following element:

    a. “name”: “John”,“Pablo”

    b. “age”: “22”, “30”

    c. “grade” : “A”, “C”

    df_student<- data.frame( 
      name = c("John", "Pablo"),
      age = c(22, 30), 
      grade = c("A", "C")
      ) 
    df_student
       name age grade
    1  John  22     A
    2 Pablo  30     C
  6. Create a numeric vector “vec_num” with the values “5,10,15,20”

    vec_num<- c(5, 10, 15, 20) 
    vec_num
    [1]  5 10 15 20
  7. Extract the second and third element from “vec_num”

    vec_num[2:3]
    [1] 10 15
  8. Create a vector “vec_seq” that contains a sequence of number from 1 to 10.

    vec_seq<- seq(1:10)
  9. Create vector “vec_rand” with 5 random numbers between 1 and 100

    vec_rand<- sample(1:100, 5) 
    vec_rand
    [1] 77  8 28 57 47
  10. Create a character vector “vec_char” with the values “apple”, “banana”, “cherry”

    vec_char<- c("apple", "banana", "cherry") 
    vec_char
    [1] "apple"  "banana" "cherry"
  11. Use the substr() function to extract the first 3 character from each elements of “vec_char”

    substr(vec_char, 1, 3)
    [1] "app" "ban" "che"
  12. Create a character vector “vec_colors” with the values “red”, “blue”, “green”, “red”, “blue”.

    vec_colors<- c("red","blue","green","red","blue")
    vec_colors
    [1] "red"   "blue"  "green" "red"   "blue" 
  13. Convert “vet_colors” into a factor vector “factor_colors”.

    factor_colors<- factor(vec_colors) 
    factor_colors
    [1] red   blue  green red   blue 
    Levels: blue green red
  14. Print the levels of “factor_colors”.

    levels(factor_colors)
    [1] "blue"  "green" "red"  
  1. Given a numeric vector “vec_bonus = c(10, 20, 30, 40, 50)” write a code snippet to

    a. Extract all elements greater than 25.

    b. Calculate the mean of the extracted elements.

    vec_bonus<- c(10, 20, 30, 40, 50) 
    #a  
    vec_bonus[vec_bonus>25] 
    [1] 30 40 50
    #b 
    mean(vec_bonus)
    [1] 30
  2. Given the following vectors:

    weight<- c(55, 68, 72, 61, 58, 75, 64, 70)

    names<- c(“Alice”, “Bob”, “Charlie”, “Daisy”, “Eve”, “Frank”, “Grace”, “Henry”)

    a. identify the names of individual whose weight is greater than 65 kg.

    b. Find the average weight of individuals weighting less tham or equal 60 kg.

    c. Extract the weight of individuals whose names start with the letter “A” or “D”

    d. Determine the number of individuals whose weight is between 60kg and 70kg. (inclusive)

    weight <- c(55, 68, 72, 61, 58, 75, 64, 70) 
    names <- c("Alice", "Bob", "Charlie", "Daisy", "Eve", "Frank", "Grace", "Henry") 
    #a 
    names[weight>65] 
    [1] "Bob"     "Charlie" "Frank"   "Henry"  
    #b 
    mean(weight[weight<=60]) 
    [1] 56.5
    #c 
    weight[substr(names, 1, 1)%in% c("A", "D")] 
    [1] 55 61
    #d
    sum(weight>=60 & weight<=70)
    [1] 4
  3. Given the following vector of daily temperatures (in celcius) for a week:

    temperatures<- c(22, 25,19, 21, 18, 24, 23)

    days<- c(“Monday”, “Tuesday”, “Wednesday”, ’Thursday”, “Friday”, “Saturday”, “Sunday”

    a. Identify the days when the temperatures was below 20

    b. Calculate the difference between the highest and lowest temperatures of the week

    c. Determine the number of days when the temperature was between 20 and 24 (inclusive)

    temperatures <- c(22, 25, 19, 21, 18, 24, 23) 
    days <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
    #a 
    days[temperatures<20] 
    [1] "Wednesday" "Friday"   
    #b 
    max(temperatures)-min(temperatures) 
    [1] 7
    #c 
    sum(temperatures>=20 & temperatures <= 24)
    [1] 4
  4. Given the following vectors of exam score:

    stores<- c(85, 78, 92, 65, 88, 70, 95, 80, 60, 90)

    students<- c(“Anna”, “Ben”, “Cara”, “Dan”, “Ella”, “Finn”, “Grace”, “Hank”, “Ivy”, “)

    a. Identify the students who scored above 90

    b. Find the average score of students who scored below 80.

    c. Extract the scores of students whoese names start with a vowel (A, E, I, O, U)

    scores <- c(85, 78, 92, 65, 88, 70, 95, 80, 60, 90) 
    students <- c("Anna", "Ben", "Cara", "Dan", "Ella", "Finn", "Grace", "Hank", "Ivy", "Jack") 
    #a 
    students[scores > 90] 
    [1] "Cara"  "Grace"
    #b 
    mean(scores[scores < 80]) 
    [1] 68.25
    #c 
    scores[substr(students, 1, 1) %in% c("A", "E", "I", "O", "U")]
    [1] 85 88 60

Take Home Exercise

  1. Basic Vector Creation: Create a numeric vector containing the numbers from 10 to 20 and a character vector containing the days of the week. Display both vectors.

    num_vec<- 10:20 
    num_vec  
     [1] 10 11 12 13 14 15 16 17 18 19 20
    days_vec<- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
    days_vec
    [1] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Saturday" 
    [7] "Sunday"   
  2. Vector Concatenation: Given two vectors: “a <- c(1, 2, 3)” and “b <- c(4, 5, 6)”, concatenate them to form a single vector.

    a<- c(1, 2, 3) 
    b<- c(4, 5, 6) 
    c(a,b)
    [1] 1 2 3 4 5 6
  3. Element Extraction: Given the vector “temperatures <- c(15, 18, 20, 22, 19, 17, 16)”, extract the temperatures for the 2nd, 4th, and 6th days.

    temperatures<- c(15, 18, 20, 22, 19, 17, 16) 
    temperatures[c(2, 4, 6)]
    [1] 18 22 17
  4. Omitting Elements: From the same “temperatures” vector, omit the temperature of the 5th day.

    temperatures[-5]
    [1] 15 18 20 22 17 16
  5. Vector Arithmetic: Given the vector “prices <- c(10, 20, 30, 40, 50)”, calculate the new prices after a 10% discount.

    prices<- c(10, 20, 30, 40, 50) 
    prices *0.9
    [1]  9 18 27 36 45
  6. Patterned Vectors: Create a vector that contains the numbers from 5 to 50, but only includes every 5th number (i.e., 5, 10, 15, …).

    seq(from = 5, to = 50, by = 5)
     [1]  5 10 15 20 25 30 35 40 45 50
  7. Random Patterns: Simulate the results of rolling a 6-sided die 10 times.

    sample(1:6, size = 10, replace = TRUE)
     [1] 3 6 5 1 6 4 2 2 6 5
  8. Character Vector Operations: Given the vector “fruits <- c(”apple”, “banana”, “cherry”)“, extract the first three letters of each fruit.

    vec_char<- c("apple", "banana", "cherry") 
    substr(vec_char, 1, 3)
    [1] "app" "ban" "che"
  9. Logical Vector Filtering: Given the vector “ages <- c(25, 30, 35, 40, 45, 50)”, identify which ages are greater than 30 and less than 50.

    ages<- c(25, 30, 35, 40, 45, 50) 
    ages[ages>30 & ages<50]
    [1] 35 40 45
  10. Relational Operators with Vectors: Given two vectors: “vector1 <- c(5, 10, 15)” and “vector2 <- c(10, 10, 20)”, determine which elements of “vector1” are less than or equal to the corresponding elements in “vector2”.

    vector1<- c(5, 10, 15) 
    vector2<- c(10, 10, 20)
    vector1<=vector2 
    [1] TRUE TRUE TRUE
    vector1[vector1<=vector2]
    [1]  5 10 15