##1. Write commands to perform below calculations:##
y = 4**2 
2**2+y   
## [1] 20
3**3     
## [1] 27
z = (4+5)**0.5 
z**2
## [1] 9
log(100,base=10)
## [1] 2
x=10**-3
3.91*x
## [1] 0.00391
######################################################
#2. What is the sum of the first 100 positive integers?#
#The formula for the sum of integers 1 through n is n(n+1)/2.##
#Define n=100 and then use R to compute the sum# 
#of 1 through 100 using the formula. What is the sum?#
#Write the command and output.#
n <- 100
sum_of_integers <- n * (n + 1) / 2
print(sum_of_integers)
## [1] 5050
##The sum of the first 100 positive integers is 5050.#
#############################################################
#3. Declare below variables to represent student information.#
studentid <- 567001
name <- "John"
gender <- "Male"
reg_course <- "Calculus"
zip <- 37203
#After declaration, write commands to determine the type of each#
#variable and perform below operations:#
print(class(studentid)) #numeric#
## [1] "numeric"
print(class(name))      #character#
## [1] "character"
print(class(gender))    #character#
## [1] "character"
print(class(reg_course))#character#
## [1] "character"
print(class(zip))       #numeric#
## [1] "numeric"
#Convert studentid to character#
studentid_char <- as.character(studentid)
print(class(studentid_char)) #character#
## [1] "character"
# Convert zip to logical
zip_logical <- as.logical(zip)
print(class(zip_logical))    #logical#
## [1] "logical"
###########################################################################
#4. Define the following vectors, which represent the weight and#
#height of people in a particular team (in inches and pounds):#
height <- c(59, 60, 61, 58, 67, 72, 70)
weight <- c(150, 140, 180, 220, 160, 140, 130)
#Calculate the length of the vectors#
length_of_height <- length(height)
length_of_weight <- length(weight)
print(paste("Length of height:", length_of_height)) #Length of height: 7#
## [1] "Length of height: 7"
print(paste("Length of weight:", length_of_weight)) #Length of weight: 7#
## [1] "Length of weight: 7"
#Calculate the sum of the heights#
sum_height <- sum(height)
print(paste("Sum of heights:",sum_height))  #Sum of heights: 447#
## [1] "Sum of heights: 447"
#Compute the average of both height and weight#
avg_height <- sum_height/length_of_height
avg_weight <- sum(weight)/length_of_weight
print(paste("Average height is:", avg_height)) #Average height is: 63.86#
## [1] "Average height is: 63.8571428571429"
print(paste("Average weight is:", avg_weight)) #Average weight is: 160#
## [1] "Average weight is: 160"
#Compute the max height
maxH <- max(height)
print(paste("The max height is:", maxH))  #The max height is: 72#
## [1] "The max height is: 72"
#Compute the minimum weight#
minW <- min(weight)
print(paste("The min weight is", minW))
## [1] "The min weight is 130"
#Create a new vector with weight +5#
new_weight <- weight + 5
#Compute the ratio weight/height for each person.#
weight_height_ratio <- new_weight/ height
print("Weight/height ratio is:") #Weight/height ratio is:#
## [1] "Weight/height ratio is:"
print(weight_height_ratio) #2.627119 2.41667 3.032787 3.879310 2.462687 2.013889 1.92857#
## [1] 2.627119 2.416667 3.032787 3.879310 2.462687 2.013889 1.928571
############################################################
#5. Use the function C() to create a vector with the#
#average high temperatures in January for Beijing, Lagos#
#Paris, Rio de Janeiro, San Juan, and Toronto, which are#
#35, 88, 42, 84, 81, and 30 degrees Fahrenheit.#
temp <- c(35, 88, 42, 84, 81, 30)
#Vector of city names#
city <- c("Beijing", "Lagos", "Paris", "Rio de Janeiro", "San Juan", "Toronto")
#Access temp of the first three cities#
temp[1:3] #35 88 42#
## [1] 35 88 42
#Access the temperatures of Paris and San Juan#
temp[c(3,5)] #42 81#
## [1] 42 81
####################################################################
#6. Create a vector of numbers that starts at 6, does not pass 55, and#
#adds numbers in increments of 4/7#
#Create the vector.#
vec_seq <- seq(6, 55, by = 4/7)
#Count the number of elements in the list#
num_elements <- length(vec_seq)
print(paste("Number of elements:", num_elements)) #Number of elements: 86#
## [1] "Number of elements: 86"
#Determine the class of the object#
a <- seq(1, 10, 0.5)
class_a <- class(a)
print(paste("Class of object a is:", class_a)) #Class of object a is: numeric#
## [1] "Class of object a is: numeric"
#Create the sequence using the : operator#
seq_numbers <- 12:73
#Repeat each number 5 times#
rep_numbers <- rep(c(34, 45, 56, 78), each =5)
################################################################################
#7. Use the function c() to create a vector x1 with values 34, 45, 56, 78, 90,#
#36, 76, 89 and vector x2 with values "a", "b", "c", "d" and write commands to #
#perform the following operations:#
#Create vectors#
x1 <- c(34, 45, 56, 78, 90, 36, 76, 89)
x2 <- c("a", "b", "c", "d")
#Compute mean, median, standard deviation, variance, min and max of vector x1#
mean_x1 <- mean(x1)
print(paste("The mean of vector x1 is:",mean_x1))
## [1] "The mean of vector x1 is: 63"
#The mean of vector x1 is: 63#
median_x1 <- median(x1)
print(paste("The median of x1 is:", median_x1))
## [1] "The median of x1 is: 66"
#The median of vector x1 is: 66#
sd_x1 <- sd(x1)
print(paste("The standard deviation of x1 is:", sd_x1))
## [1] "The standard deviation of x1 is: 23.1208007770369"
#The standard deviation of x1 is: 23.1208007770369#
var_x1 <- var(x1)
print(paste("The variance of x1 is:", var_x1))
## [1] "The variance of x1 is: 534.571428571429"
#The variance of x1 is: 534.571428571429#
min_x1 <- min(x1)
print(paste("The minimum value of x1 is:", min_x1))
## [1] "The minimum value of x1 is: 34"
#The minimum value of x1 is: 34#
max_x1 <- max(x1)
print(paste("The maximum value of x1 is:", max_x1))
## [1] "The maximum value of x1 is: 90"
#The maximum value of x1 is: 90#
#Extract the 6th element of x1#
sixth_element <- x1[6]
print(paste("The 6th element of x1 is:", sixth_element))
## [1] "The 6th element of x1 is: 36"
#The 6th element of x1 is: 36#
#Extract the 4th and 8th elements of x1#
fourth_eighth <- x1[4:8]
print("The 4th to 8th elements of x1:")
## [1] "The 4th to 8th elements of x1:"
print(fourth_eighth)
## [1] 78 90 36 76 89
#The 4th to 8th elements of x1: 78 90 36 76 89#
#Extract values greater than 30 and less than 60 in x1#
extracted_values <- x1[x1>30 & x1<60]
print("Values greater than 30 and less than 60 in x1:")
## [1] "Values greater than 30 and less than 60 in x1:"
print(extracted_values)
## [1] 34 45 56 36
#Values greater than 30 and less than 60 in x1: 34 45 56 36#
#########################################################
#9.Load the US gun murders dataset by following below steps.#
#Download the file murders.csv from blackboard#
#Copy it in the location where your script is located.#
#Write below commands on your script to read the data file.#
murder_data <- read.csv("murders.csv", stringsAsFactors = F)
#Determine the number columns and rows#
num_rows <- nrow(murder_data)
num_cols <- ncol(murder_data)
print(paste("Number of rows:", num_rows))
## [1] "Number of rows: 51"
print(paste("Number of columns:", num_cols))
## [1] "Number of columns: 5"
#Extract elements at 3rd row and 4th columns#
element_3_4 <- murder_data[3, 4]
print(paste("Element at 3rd row and 4th column:", element_3_4))
## [1] "Element at 3rd row and 4th column: 6392017"
#Print summary statistics of total.gun.murders#
##Problems 8 and 9 are written in Google Colab given that I could not get R studio to load my file.##
##See second attachment for results.##
##############################################################################################################
#10. Create a list L containing character strings, numbers, data frame and matrix.#
##Please follow below rules to create the list##
###Character string vector should contain 4 lements and named as first.###
first <- c("Jaclyn", "Jackie", "Jacqueline", "Jacklynn")
#numeric vector from 10 to 50
second <- seq(10,50)
#data frame
third <- data.frame(
  p1 = c(1, 2, 3, 4),
  p2 = c("A","E","I","O")
)
#Matrix
fourth <- matrix(1:12, nrow = 4, byrow = TRUE)
#Create the list L
L <- list(first=first, second = second, third=third, fourth=fourth)
#Retrieving the data fram
L$third
##   p1 p2
## 1  1  A
## 2  2  E
## 3  3  I
## 4  4  O
#Retrieving the matrix
L$fourth
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
## [4,]   10   11   12
#Add a numeric vector element with name fifth
L$fifth <- c(5, 10, 15, 20)
#print the updated list#
print(L)
## $first
## [1] "Jaclyn"     "Jackie"     "Jacqueline" "Jacklynn"  
## 
## $second
##  [1] 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
## [26] 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
## 
## $third
##   p1 p2
## 1  1  A
## 2  2  E
## 3  3  I
## 4  4  O
## 
## $fourth
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
## [4,]   10   11   12
## 
## $fifth
## [1]  5 10 15 20