exercise 1 - Vector

Author

Aishah Natasha Binti Aziz

Exercise 1

  1. create a numeric variable num_var with the value 42.5
num_var <-42.5
num_var
[1] 42.5
  1. create a character variable char_var with the value “R is fun!”
char_var <- "R is fun!"
char_var
[1] "R is fun!"
  1. print the data type of char_va.
class(char_var)
[1] "character"
  1. create a list “student_info” with the following elements:
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
  1. create a data frame df_student
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
  1. create a numeric vector “vec_num” with the value “5,10,15,20”.
vec_num <- c(5,10,15,20)
vec_num
[1]  5 10 15 20
  1. extract the second and third element from “vec_num”.
vec_num[c(2,3)]
[1] 10 15
  1. 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
  1. create a vector “vec_rand” with 5 random numbers between 1 and 100.
vec_rand <- sample(1:100, 5)
vec_rand
[1] 59 95 18 78 40
  1. create a character vector “vec_char” with the value “apple”, “banana”, “cherry”
vec_char <- c("apple", "banana", "cherry")
vec_char
[1] "apple"  "banana" "cherry"
  1. 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"
  1. created a character vector “vec_colors” with the value of “red”. “blue”, “green”, “red”,“blue”.
vec_colors <- c("red", "blue", "green", "red", "blue")
vec_colors
[1] "red"   "blue"  "green" "red"   "blue" 
  1. convert “vec_colors” into a factor vector “factor_colors”
factor_colors <- as.factor(vec_colors)
factor_colors
[1] red   blue  green red   blue 
Levels: blue green red
  1. print the levels of “factor_colors”
levels(factor_colors)
[1] "blue"  "green" "red"  
  1. given the numeric vector “vec_bonus = c(10,20,30,40,50)”, write a code snippet to :

a) extract all elements greater than 25.

vec_bonus = c(10,20,30,40,50)
bonus_extract <- vec_bonus[vec_bonus > 25]
bonus_extract
[1] 30 40 50

b) calculate the mean of the extracted elements.

mean(bonus_extract)
[1] 40
  1. given the following vectors:
weights <- c(55, 68, 72, 61, 58, 75, 64, 70)

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

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

names[weights > 65]
[1] "Bob"     "Charlie" "Frank"   "Henry"  

b. find the average weights of individuals weighting less than or equal to 60kg.

mean(weights[weights <= 60])
[1] 56.5

c.. extract the weights of individuals whose names starts with the letter ‘A’ or ‘D’.

weights[substr(names,1,1) %in% c("A", "D")]
[1] 55 61

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

sum(weights >= 60 & weights <= 70)
[1] 4
  1. given the following vector of daily temperatures (in Celsius) 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 20C

days[temperatures < 20]
[1] "Wednesday" "Friday"   

b) calculate the difference between the highest and lowest temperatures of the week.

max(temperatures) - min(temperatures)
[1] 7

c) determine the number of days when the temperature was between 20C and 24C

sum(temperatures >= 20 & temperatures <= 24)
[1] 4
  1. given the following vector of exam scores:

    scores <- 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

students[scores > 90]
[1] "Cara"  "Grace"

b) find the average score of students who scored below 80.

mean(scores[scores < 80])
[1] 68.25

c) extract the scores of students whose names start with a vowel (A,E,I,O,U)

scores[substr(students,1,1) %in% c("A", "E", "I", "O","U")]
[1] 85 88 60 90

TAKE HOME EXERCISE

  1. create a numeric vector containing the numbers10 to 20 and a character vector containing the days of the week. Display both vectors.

    num_vec <- 10:20
    days_vec <- c("Monday", "Tuesday", "WedneSday", "Thursday", "Friday", "Saturday", "Sunday")
    
    num_vec
     [1] 10 11 12 13 14 15 16 17 18 19 20
    days_vec
    [1] "Monday"    "Tuesday"   "WedneSday" "Thursday"  "Friday"    "Saturday" 
    [7] "Sunday"   
  2. 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_vec <- c(a , b)
c_vec
[1] 1 2 3 4 5 6
  1. given the vector temperatures <- c(15, 18, 20, 22, 19, 17, 16), extract the temperatures for the 2nd, 4th, and 6th days.
::: {.cell}

```{.r .cell-code}
temperatures <- c(15, 18, 20, 22, 19, 17, 16)
extract_temps <- temperatures[c(2,4,6)]
extract_temps
```

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

```
[1] 18 22 17
```


:::
:::
  1. from the same temperatures vectors, omit the temperature of the 5th day.

    omitted_temps <- temperatures[-5]
    omitted_temps
    [1] 15 18 20 22 17 16
  2. given the vector prices <- c(10,20,30,40,50) calculate the new prices after 10% discount.

    prices <- c(10,20,30,40,50)
    disc_prices <- prices * 0.90
    disc_prices
    [1]  9 18 27 36 45
  3. created a vector that contains the numbers from 5 to 50, but only includes every 5th number (ie., 5,10,15)

pat_vec <- seq(from = 5,to = 50, by = 5)
pat_vec
 [1]  5 10 15 20 25 30 35 40 45 50
  1. simulate the results of rolling a 6- sides die 20 times.

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

fruits <- c("apple" , "banana", "cherry")
first_three <- substr(fruits, start =1, stop = 3)
first_three
[1] "app" "ban" "che"
  1. 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)
    vec_ages <- ages[ages > 30 & ages < 50]
    vec_ages 
    [1] 35 40 45
  2. 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 coressponding elements in vector2.

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