Exercise 1

Author

Auni Damia Binti Ahmad Firdaus

Exercise 1

  1. Create a numeric variable ” num var ” with the value “42.5”.
::: {.cell}

```{.r .cell-code}
num_var <- c(42.5)
num_var
```

::: {.cell-output .cell-output-stdout}

```
[1] 42.5
```


:::
:::
  1. Create a character variable “char_var” with the value “R is fun!”.

    char_var <- c("R is fun!")
    char_var
    [1] "R is fun!"
  2. Print the data type “char_var”.

    class(char_var)
    [1] "character"
  3. Create a list “student_info” with the following elements :

    1. “name” : “Mohammad Nasir Abdullah”
    2. “age” : 18
    3. “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
  4. Create a data frame “df_students” with the following columns

    1. “Name” : “John”, “Pablo”
    2. “Age” : “22”, “30”
    3. “Grade” : “A”, “C”
    df_students <- data.frame (
        name = c("John", "Pablo"),
        age = c("22","30"),
        grade = c("A", "C")
    )
    df_students
       name age grade
    1  John  22     A
    2 Pablo  30     C
  5. Create 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
  6. Extract the second and third elements from ” vec_num”.

    vec_num[c(2,3)]
    [1] 10 15
  7. Create a vector ” vec_seq” that contains a sequence of number from 1 to 10.

    vec_seq <- (1:10)
    vec_seq
     [1]  1  2  3  4  5  6  7  8  9 10
  8. Create a vector ” vec_rand” with 5 random numbers between 1 and 100.

    vec_rand <- sample(1:100,size = 5)
    vec_rand
    [1] 21 10 72 57 98
  9. Create a character vector ” vec_char” with the values “apple”, “banana”, “cherry”.

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

    substr(vec_char,1,3)
    [1] "app" "ban" "che"
  11. 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" 
  12. Convert ” vec_colors” into a factor vector ” factor_colors”.

    factor_colors <- as.factor(vec_colors)
    class(factor_colors)
    [1] "factor"
  13. Print the levels of “factor_colors”

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

    1. Extract all elements greater than 25.
    2. Calculate the mean of the extracted elements.
    vec_bonus <- c(10,20,30,40,50)
    vec_bonus[vec_bonus > 25]
    [1] 30 40 50
    mean(vec_bonus)
    [1] 30
  15. Given the following vectors :

    1. Identify the names of individuals whose weight is greater than 65kg.
    2. Find the average weight of individuals weighting less than or equal to 60kg.
    3. Extract the weights of individuals whose names start with the letter ‘A’ or ‘D’.
    4. Determine the number of individuals whose weight is between 60kg and 70kg (inclusive).
    weights <- c(55, 68, 72, 61, 58, 75, 64, 70)
    names <- c("Alice", "Bob", "Charlie", "Daisy", "Eve", "Frank", "Grace", "Henry")
    names [weights > 65]
    [1] "Bob"     "Charlie" "Frank"   "Henry"  
    mean (weights <= 60)
    [1] 0.25
    weights[startsWith(names, "A") | startsWith(names, "D")]
    [1] 55 61
    length(names[weights > 60 & weights < 70])
    [1] 3
  16. Given the following vector of daily temperatures (in Celsius) for a week :

    1. Identify the days when the temperature was below 20°C.
    2. Calculate the difference between the highest and lowest temperatures of the week.
    3. Determine the number of days when the temperature was between 20°C and 24°C (inclusive).
    temperatures <- c(22, 25, 19, 21, 18, 24, 23)
    days <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
    days [temperatures < 20]
    [1] "Wednesday" "Friday"   
    diff (range(temperatures))
    [1] 7
    length(days[temperatures > 20 & temperatures < 24])
    [1] 3
  17. Given the following vector of exam scores :

    1. Identify the students who scored above 90.
    2. Find the average score of students who scored below 80.
    3. Extract the scores of students whose 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","John")
    students[scores > 90]
    [1] "Cara"  "Grace"
    mean(scores < 80)
    [1] 0.4
    scores[substr(students,1,1) %in% c("A","E","I","O","U")]
    [1] 85 88 60
  18. 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.

    numeric_vector <- c(10,11,12,13,14,15,16,17,18,19,20)
    character_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
  19. 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 <- c(a,b)
    c
    [1] 1 2 3 4 5 6
  20. 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
  21. Omitting Elements: From the same temperatures vector, omit the temperature of the 5th day.

::: {.cell}

```{.r .cell-code}
temperatures[5]
```

::: {.cell-output .cell-output-stdout}

```
[1] 19
```


:::
:::
  1. 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)
    discount <- prices*0.1
    new_total <- prices - discount
    new_total
    [1]  9 18 27 36 45
  2. Patterned Vectors: Create a vector that contains the numbers from 5 to 50, but only includes every 5th number (i.e., 5, 10, 15,…).

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

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

    fruits <- c ("apple","banana", "cherry")
    substr(fruits,1,3)
    [1] "app" "ban" "che"
  5. 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] 30 35 40 45 50
  6. Relational Operators with Vectors: Given two vectors: vector1 ‹- c(5, 10, 15) and vector2 ‹- c(10, 10, 20), determine which elements of vector 1 are less than or equal to the corresponding elements in vector2.

::: {.cell}

```{.r .cell-code}
vector1 <- c(5, 10, 15)
vector2 <- c(10, 10, 20)
vector2[vector2 <= vector1]
```

::: {.cell-output .cell-output-stdout}

```
[1] 10
```


:::
:::