bs5th<-data.frame(name=c("ali","ahmad","sania,","sana","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),grades=c("D","B","A","c","B","A","B","B","B","A","D"))
bs5th
## name cgpa grades
## 1 ali 2.50 D
## 2 ahmad 3.20 B
## 3 sania, 3.90 A
## 4 sana 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
##……………………………Question no.1…………………………. ## which students have cgpa greater then or equL TO 3.10
three_above<-bs5th$cgpa>=3
three_above
## [1] FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE
bs5th$name[three_above]
## [1] "ahmad" "sania," "adil" "asad" "hassan"
##Which students got B grade
b_grade<-bs5th$grades=="B"
b_grade
## [1] FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE TRUE FALSE FALSE
bs5th$name[b_grade]
## [1] "ahmad" "adil" "subhan" "arbaz" "asad"
##Which students have cgpa 3 above or have grade A
legend<-bs5th$cgpa>3 & bs5th$grades=="A"
legend
## [1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
bs5th$name[legend]
## [1] "sania," "hassan"
##……………Question no.01………………………………………. ##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.
library(dslabs)
data(murders)
head(murders)
## state abb region population total
## 1 Alabama AL South 4779736 135
## 2 Alaska AK West 710231 19
## 3 Arizona AZ West 6392017 232
## 4 Arkansas AR South 2915918 93
## 5 California CA West 37253956 1257
## 6 Colorado CO West 5029196 65
murder_rate <- (murders$total / murders$population) * 100000
low <- murder_rate < 1
low
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
## [13] TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE
## [25] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
## [37] FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE FALSE
## [49] FALSE FALSE TRUE
murders$state[low]
## [1] "Hawaii" "Idaho" "Iowa" "Maine"
## [5] "Minnesota" "New Hampshire" "North Dakota" "Oregon"
## [9] "South Dakota" "Utah" "Vermont" "Wyoming"
murder_rate
## [1] 2.8244238 2.6751860 3.6295273 3.1893901 3.3741383 1.2924531
## [7] 2.7139722 4.2319369 16.4527532 3.3980688 3.7903226 0.5145920
## [13] 0.7655102 2.8369608 2.1900730 0.6893484 2.2081106 2.6732010
## [19] 7.7425810 0.8280881 5.0748655 1.8021791 4.1786225 0.9992600
## [25] 4.0440846 5.3598917 1.2128379 1.7521372 3.1104763 0.3798036
## [31] 2.7980319 3.2537239 2.6679599 2.9993237 0.5947151 2.6871225
## [37] 2.9589340 0.9396843 3.5977513 1.5200933 4.4753235 0.9825837
## [43] 3.4509357 3.2013603 0.7959810 0.3196211 3.1246001 1.3829942
## [49] 1.4571013 1.7056487 0.8871131
##………………………….Question no .02……………………….. ##. 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
## [1] 12 13 16 20 24 30 35 38 42 45 46 51
##………………………………..Question no.03……………………… ##3. Use the results from the previous exercise to report the names of the states with murder rates lower than 1.
states_with_low_murder_rate <- murders$state[low_indices]
states_with_low_murder_rate
## [1] "Hawaii" "Idaho" "Iowa" "Maine"
## [5] "Minnesota" "New Hampshire" "North Dakota" "Oregon"
## [9] "South Dakota" "Utah" "Vermont" "Wyoming"
##………………………….Question no.04………………………….. ##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 defned logical vector low and the logical operator &.
# 4. Report the states in the Northeast with murder rates lower than 1
northeast_states <- c("Connecticut", "Maine", "Massachusetts", "New Hampshire", "New Jersey", "New York", "Pennsylvania", "Rhode Island", "Vermont")
northeast_low_indices <- which(low & murders$state %in% northeast_states)
northeast_states_with_low_murder_rate <- murders$state[northeast_low_indices]
northeast_states_with_low_murder_rate
## [1] "Maine" "New Hampshire" "Vermont"
##…………………………………..Question no.05…………………. ##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?
average_murder_rate <- mean(murder_rate)
states_below_average <- sum(murder_rate < average_murder_rate)
states_below_average
## [1] 27
##……………………………Question no.06…………………………. ##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.
abbreviations_to_find <- c("AK", "MI", "IA")
indices <- match(abbreviations_to_find, murders$abb)
states_with_abbreviations <- murders$state[indices]
states_with_abbreviations
## [1] "Alaska" "Michigan" "Iowa"
##…………………………Question no.07…………………………. ##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 ?
# 7. Create a logical vector to identify actual abbreviations
abbreviations_to_check <- c("MA", "ME", "MI", "MO", "MU")
actual_abbreviations <- abbreviations_to_check %in% murders$abb
actual_abbreviations
## [1] TRUE TRUE TRUE TRUE FALSE
murders$state[actual_abbreviations]
## [1] "Alabama" "Alaska" "Arizona"
## [4] "Arkansas" "Colorado" "Connecticut"
## [7] "Delaware" "District of Columbia" "Georgia"
## [10] "Hawaii" "Idaho" "Illinois"
## [13] "Iowa" "Kansas" "Kentucky"
## [16] "Louisiana" "Maryland" "Massachusetts"
## [19] "Michigan" "Minnesota" "Missouri"
## [22] "Montana" "Nebraska" "Nevada"
## [25] "New Jersey" "New Mexico" "New York"
## [28] "North Carolina" "Ohio" "Oklahoma"
## [31] "Oregon" "Pennsylvania" "South Carolina"
## [34] "South Dakota" "Tennessee" "Texas"
## [37] "Vermont" "Virginia" "Washington"
## [40] "West Virginia" "Wyoming"
##. 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.
# 8. Report the entry that is not an actual abbreviation
not_actual_abbreviation_index <- which(!actual_abbreviations)
not_actual_abbreviation <- abbreviations_to_check[not_actual_abbreviation_index]
not_actual_abbreviation
## [1] "MU"