vectors:
Create a vector containing elements 10, 22, 27, 19, 20 and assign it with a name.
# your code here
my_vec <- c(10, 22, 27, 19, 20)
Use R as a calculator to compute the following values. a). 27(38-15) b). ln(14^7) c). sqrt(436/12)
# your code here
a <- 27 * (38-15)
a
## [1] 621
library(SciViews)
b <- ln(14^7)
b
## [1] 18.4734
c <- sqrt(436/12)
c
## [1] 6.027714
# your code here
b <- c()
x <- 87
for (i in 87:56) {
x <- x - 1
b <- append(b, i)
}
val <- b[19:21]
val
## [1] 69 68 67
compute the following statistics of b: 1) sum 2) median 3) standard deviation
# your code here
add <- sum(b)
add
## [1] 2288
med <- median(b)
med
## [1] 71.5
standard_deviation <- sd(b)
standard_deviation
## [1] 9.380832
Create a vector that contains 100 elements with value of 1. (Hint: use ?rep for help). Use the option results = ’hide” in the code chuck to hide the result
# your code here
vec <- rep(1, 100)
Create a matrix with 16 elements with 4 rows both by row and column wise store it as Ex1 and Ex2
Access 3rd row 3rd column element of the matrices
Access 2nd row 1st column element of them
# your code here
Ex1 <- matrix(1:8, nrow = 2)
Ex2 <- matrix(9:16, nrow = 2)
my_matrix <- rbind(Ex1, Ex2)
my_matrix[3, 3]
## [1] 13
my_matrix[2, 1]
## [1] 2
Imagine you have data on temperature readings for different cities In Africa for 3 months. How would you create a matrix in R to store this data, with cities as rows and months as columns?
# your code here
Mombasa <- c(27, 25, 31)
Juba <- c(26, 24, 23)
Kampala <- c(28, 26, 27)
months <- c("January", "February", "March")
city_names <- c(Mombasa, Juba, Kampala)
names(Mombasa) <- months
names(Juba) <- months
names(Kampala) <- months
reading_matrix <- matrix(city_names, byrow = TRUE, nrow = 3)
cities <- c("Mombasa", "Juba", "Kampala")
rownames(reading_matrix) <- cities
colnames(reading_matrix) <- months
reading_matrix
## January February March
## Mombasa 27 25 31
## Juba 26 24 23
## Kampala 28 26 27
You need to calculate the average temperature for each city over the 3 months. How would you access and manipulate specific elements of the matrix in R to achieve this? Write the code for both row and column-wise calculations
# your code here
avg_mombasa <- mean(reading_matrix[1,])
avg_mombasa
## [1] 27.66667
avg_juba <- mean(reading_matrix[2,])
avg_juba
## [1] 24.33333
avg_kampala <- mean(reading_matrix[3,])
avg_kampala
## [1] 27
avg_january <- mean(reading_matrix[,1])
avg_january
## [1] 27
avg_february <- mean(reading_matrix[,2])
avg_february
## [1] 25
avg_march <- mean(reading_matrix[,3])
avg_march
## [1] 27
Suppose you have information about different employees, including their name, department, age, and salary. How would you create a data frame in R to store this data? Include different data types for each variable.
# your code here
data <- data.frame(name = c("Mason","Mark", "Jason", "Gabriel","Samir", "Alvin"), department = c("Marketing", "Tech-supprot","Marketing","Tech-supprot", "Transport", "Marketing"), age = c(27, 31, 42,35, 26, 29), salary = c(55000, 45000,52000, 60000,93000, 35000))
data
## name department age salary
## 1 Mason Marketing 27 55000
## 2 Mark Tech-supprot 31 45000
## 3 Jason Marketing 42 52000
## 4 Gabriel Tech-supprot 35 60000
## 5 Samir Transport 26 93000
## 6 Alvin Marketing 29 35000
You want to filter the data frame to find employees in the “Marketing” department with a salary above Ksh50,000. Write the code using appropriate indexing and logical operators.
# your code here
marketing_employees <- data.frame()
for (i in 1:nrow(data)) {
if (data[i, "department"] == "Marketing" && data[i, "salary"] > 50000) {
marketing_employees <- rbind(marketing_employees, data[i, ])
}
}
marketing_employees
## name department age salary
## 1 Mason Marketing 27 55000
## 3 Jason Marketing 42 52000
Create a list that stores different types of data: a numeric vector, a character string, and another list.
# your code here
my_new_list <- list(c(5, 6, 11), "hello", list(TRUE, 15))
my_new_list
## [[1]]
## [1] 5 6 11
##
## [[2]]
## [1] "hello"
##
## [[3]]
## [[3]][[1]]
## [1] TRUE
##
## [[3]][[2]]
## [1] 15
You want to add a new element (a logical value) to the end of the list. How would you achieve this? Include different methods.
# your code here
my_new_list <- c(my_new_list, FALSE)
append(my_new_list, TRUE)
## [[1]]
## [1] 5 6 11
##
## [[2]]
## [1] "hello"
##
## [[3]]
## [[3]][[1]]
## [1] TRUE
##
## [[3]][[2]]
## [1] 15
##
##
## [[4]]
## [1] FALSE
##
## [[5]]
## [1] TRUE
my_new_list <- c(my_new_list[-1], FALSE)
my_new_list
## [[1]]
## [1] "hello"
##
## [[2]]
## [[2]][[1]]
## [1] TRUE
##
## [[2]][[2]]
## [1] 15
##
##
## [[3]]
## [1] FALSE
##
## [[4]]
## [1] FALSE
Imagine you have survey data where participants chose their favorite R packages from a set of options. How would you create a factor variable in R to store this data?
# your code here
survey_output <- data.frame(names= c("Jack", "John", "Hans","Brian"), packages = c("ggplot2", "dplyr", "tidyr", "purrr"))
formats <- unique(survey_output$packages)
favorite_packages <- factor(survey_output$packages, levels = formats)
favorite_packages
## [1] ggplot2 dplyr tidyr purrr
## Levels: ggplot2 dplyr tidyr purrr
You want to analyze the number of people who chose each color. How would you use the table function and factor levels to get a frequency table? Write the code and interpret the results.
# your code here
colors <- c("red", "blue", "green", "red", "blue", "purple")
color_factor <- factor(colors, levels = c("red", "blue", "green", "purple"))
color_counts <- table(color_factor)
# Print the frequency table
print(color_counts)
## color_factor
## red blue green purple
## 2 2 1 1
Describe the difference between is.finite(x) and !is.infinite(x).
# your answer here
#..`is.finite()` tests whether a value is **not** `-Inf` or `Inf` and `is.infinite` tests whether it is `-Inf` or `Inf`. Logically, `!` negates the previous.
#.. This function, is.finite(x), returns TRUE if x is a finite numeric value, meaning it's not NaN, Inf, or -Inf.
#.. '!is.infinite(x)', on the other hand, translates to "not infinity," returns TRUE if x is not both Inf and -Inf.