Attempt all questions and DO NOT use ChatGPT Or Bard but rather try doing if stuck reference cheat sheets from posit blog, R for data science book and ask your peers.

vectors:

Create a vector containing elements 10, 22, 27, 19, 20 and assign it with a name.

# your code here
vect1<-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
27*(38-15)
## [1] 621
log(14^7,base = exp(1))
## [1] 18.4734
sqrt(436/12)
## [1] 6.027714
  1. Create the following vectors: b = (87, 86, 85, …, 56)
  2. What is the 19th, 20th, and 21st elements of b?
# your code here
(b<-c(87:56))
##  [1] 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63
## [26] 62 61 60 59 58 57 56
b[19]
## [1] 69
b[20]
## [1] 68
b[21]
## [1] 67

compute the following statistics of b: 1) sum 2) median 3) standard deviation

# your code here
sum(b)
## [1] 2288
median(b)
## [1] 71.5
sd(b)
## [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
vect2<-rep(1,100)

Matrices:

Create a matrix with 16 elements with 4 rows both by row and column wise store it as Ex1 and Ex2

  1. Access 3rd row 3rd column element of the matrices

  2. Access 2nd row 1st column element of them

# your code here
(Ex1<-matrix(1:16,ncol = 4,byrow = T))
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    5    6    7    8
## [3,]    9   10   11   12
## [4,]   13   14   15   16
(Ex2<-matrix(1:16,ncol = 4,byrow = F))
##      [,1] [,2] [,3] [,4]
## [1,]    1    5    9   13
## [2,]    2    6   10   14
## [3,]    3    7   11   15
## [4,]    4    8   12   16
Ex1[3,3]
## [1] 11
Ex2[3,3]
## [1] 11
Ex1[2,1]
## [1] 5
Ex2[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
(temp<-matrix(c(34,23,38,36,21,40,36,12,19),ncol = 3,byrow = F))
##      [,1] [,2] [,3]
## [1,]   34   36   36
## [2,]   23   21   12
## [3,]   38   40   19
colnames(temp)<-c('January','February','March')
rownames(temp)<-c("Nairobi",'Nakuru','Eldoret')
temp
##         January February March
## Nairobi      34       36    36
## Nakuru       23       21    12
## Eldoret      38       40    19

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
colMeans(temp)
##  January February    March 
## 31.66667 32.33333 22.33333
rowMeans(temp)
##  Nairobi   Nakuru  Eldoret 
## 35.33333 18.66667 32.33333

Data Frames

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
names<-c("christiano","benzema","bale","hazzard")
age<-c(39,36,34,33)
department<-c("marketing","IT","sales","HR")
salary<-c(65000,40000,45000,39000)
(employee<-data.frame(names,age,department,salary))
##        names age department salary
## 1 christiano  39  marketing  65000
## 2    benzema  36         IT  40000
## 3       bale  34      sales  45000
## 4    hazzard  33         HR  39000

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
filter(employee,department=="marketing"&salary>50000)
## Time Series:
## Start = 1 
## End = 4 
## Frequency = 1 
##   [,1] [,2] [,3]  [,4]
## 1   NA   NA   NA    NA
## 2    4   33    1 39000
## 3   NA   NA   NA    NA
## 4   NA   NA   NA    NA

Lists:

Create a list that stores different types of data: a numeric vector, a character string, and another list.

# your code here
my_list <- list(
  numeric_vector <- c(1, 2, 3),
  character_string <- "Hello, world!",
  another_list <- list("a", "b", "c")
)

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
new_list<-list("n",c(2,3),4)
new_list<-list(new_list,TRUE)

Factors:

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_responses <- c("ggplot2", "dplyr", "tidyr", "ggplot2", "ggplot2", "dplyr")
(favorite_packages <- factor(survey_responses))
## [1] ggplot2 dplyr   tidyr   ggplot2 ggplot2 dplyr  
## Levels: dplyr ggplot2 tidyr

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
color_vector <- c("red", "blue", "green", "red", "red", "blue", "green", "yellow")
(color_freq <- table(color_vector))
## color_vector
##   blue  green    red yellow 
##      2      2      3      1

Bonus

Describe the difference between is.finite(x) and !is.infinite(x).

# your answer here

#is.finite(x) checks for finiteness, while !is.infinite(x) checks for non-infiniteness

Install swirl package by running install.packages(“swirl”) and do the the R Programming: The basics in programming in R course writing your code in a markdown file, publish it on your RPubs account and push it to your GitHub under a repository name dekut_r_sessions.