BS_5th <- data.frame(
name = c('Ali','Ahmad','Sania','Sara','Adil','Sharjeel','Subhan','Arbaz','Asad','Hassan','waleed'),
cgpa = c(2.5, 3.2, 3.9, 2.99, 3.10, 2.99, 2.91, 2.87, 3.2, 3.52, 2.50),
grade = c('D', 'B', 'A', 'C', 'B', 'A', 'B', 'B', 'B', 'A', 'D')
)
BS_5th
## name cgpa grade
## 1 Ali 2.50 D
## 2 Ahmad 3.20 B
## 3 Sania 3.90 A
## 4 Sara 2.99 C
## 5 Adil 3.10 B
## 6 Sharjeel 2.99 A
## 7 Subhan 2.91 B
## 8 Arbaz 2.87 B
## 9 Asad 3.20 B
## 10 Hassan 3.52 A
## 11 waleed 2.50 D
Q1: which student have cgpa greater than or equal to 3.10?
ind<-BS_5th$cgpa>=3.10
BS_5th$name[ind]
## [1] "Ahmad" "Sania" "Adil" "Asad" "Hassan"
Q2:which student have got “B” grade?
ind<-BS_5th$grade=='B'
BS_5th$name[ind]
## [1] "Ahmad" "Adil" "Subhan" "Arbaz" "Asad"
Q3:which student have cgpa >3.1 and got “A”?
ind_cgpa<-BS_5th$cgpa>3.1
ind_grade<-BS_5th$grade=='A'
BS_5th$name[ind_cgpa&ind_grade]
## [1] "Sania" "Hassan"
Start by loading the library and data.
library(dslabs)
data(murders)
Q:1 Compute the per 100,000 murder rate for each state and store it in an object called murder_rate. Then use logical operators to create a logical vector named low that tells us which entries of murder_rate are lower than 1.
murders_rate=(murders$total)/(murders$population)*1000000
murders_rate
## [1] 28.244238 26.751860 36.295273 31.893901 33.741383 12.924531
## [7] 27.139722 42.319369 164.527532 33.980688 37.903226 5.145920
## [13] 7.655102 28.369608 21.900730 6.893484 22.081106 26.732010
## [19] 77.425810 8.280881 50.748655 18.021791 41.786225 9.992600
## [25] 40.440846 53.598917 12.128379 17.521372 31.104763 3.798036
## [31] 27.980319 32.537239 26.679599 29.993237 5.947151 26.871225
## [37] 29.589340 9.396843 35.977513 15.200933 44.753235 9.825837
## [43] 34.509357 32.013603 7.959810 3.196211 31.246001 13.829942
## [49] 14.571013 17.056487 8.871131
low<-murders_rate<1
low
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [49] FALSE FALSE FALSE
Q:2 Now use the results from the previous exercise and the function which to determine the indices of murder_rate associated with values lower than 1.
low_indices<-which(low)
low_indices
## integer(0)
Q:3 Use the results from the previous exercise to report the names of the states with murder rates lower than 1
state<-murders$state[low_indices]
state
## character(0)
Q:4 Now extend the code from exercise 2 and 3 to report the states in the Northeast with murder rates lower than 1. Hint: use the previously defined logical vector low and the logical operator
ind_1<-murders$region=='Northeast'
ind_1
## [1] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE
## [25] FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE
## [37] FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
## [49] FALSE FALSE FALSE
ind_2<-low[ind_1]
ind_2
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
states<-murders$state[ind_1 & ind_2]
## Warning in ind_1 & ind_2: longer object length is not a multiple of shorter
## object length
states
## character(0)
Q:5 In a previous exercise we computed the murder rate for each state and the average of these numbers.How many states are below the average?
mean_murders_rate<-mean(murders_rate)
mean_murders_rate
## [1] 27.79125
states_below_avg<-sum(murders_rate<mean_murders_rate)
states_below_avg
## [1] 27
Q:6 Use the match function to identify the states with abbreviations AK, MI, and IA. Hint: start by defning an index of the entries of murders$abb that match the three abbreviations, then use the [ operator to extract the states.
ind<-match(c("AK","MI","IA"),murders$abb)
murders$state[ind]
## [1] "Alaska" "Michigan" "Iowa"
Q:7 Use the %in% operator to create a logical vector that answers the question: which of the following are actual abbreviations: MA, ME, MI, MO, MU ?
c("MA","ME","MI","MO","MU") %in% murders$abb
## [1] TRUE TRUE TRUE TRUE FALSE
Q:8 Extend the code you used in exercise 7 to report the one entry that is not an actual abbreviation.Hint: use the ! operator, which turns FALSE into TRUE and vice versa, then which to obtain an index
# Define the abbreviations
abbreviations <- c("MA", "ME", "MI", "MO", "MU")
# Check if each abbreviation is in the list of actual abbreviations
actual_abbreviations <- abbreviations %in% murders$abb
actual_abbreviations
## [1] TRUE TRUE TRUE TRUE FALSE
# Find the index of the entry that is not an actual abbreviation
index_not_actual <- which(!actual_abbreviations)
index_not_actual
## [1] 5
# Get the corresponding abbreviation
not_actual_abbreviation <- abbreviations[index_not_actual]
# Display the result
not_actual_abbreviation
## [1] "MU"