bs5th <- data.frame(
  name = c("Ali", "Ahmad", "Sania", "Sara", "Adil", "sharjeel", "subhan", "arbaz", "athar", "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", "D", "A", "B", "B", "B", "A", "D")
)

##(a) which students have cgpa greater than 3

student_with_greater_than_3_cgpa <- bs5th[bs5th$cgpa > 3, ]
print(student_with_greater_than_3_cgpa)
##      name cgpa grade
## 2   Ahmad 3.20     B
## 3   Sania 3.90     A
## 5    Adil 3.10     D
## 9   athar 3.20     B
## 10 hassan 3.52     A

##(b)which student have get “A” grade

# Assuming 'bs5th' is your data frame
students_with_A_grade <- subset(bs5th, grade == "A")
print(students_with_A_grade)
##        name cgpa grade
## 3     Sania 3.90     A
## 6  sharjeel 2.99     A
## 10   hassan 3.52     A

##(c)which student have cgpa >3 and get A

students_cgpa_A <- subset(bs5th, cgpa > 3 & grade == "A")
print(students_cgpa_A)
##      name cgpa grade
## 3   Sania 3.90     A
## 10 hassan 3.52     A

##(d)which student have cgpa >3 or get A

students_cgpa_or_A <- subset(bs5th, cgpa > 3 | grade == "A")
print(students_cgpa_or_A)
##        name cgpa grade
## 2     Ahmad 3.20     B
## 3     Sania 3.90     A
## 5      Adil 3.10     D
## 6  sharjeel 2.99     A
## 9     athar 3.20     B
## 10   hassan 3.52     A

##exercise no 3.15

 library(dslabs)
 data(murders)

##(1) Compute the per 100,000 murder rate for each state and store it in an object called murder_rate. Thenuse logical operators to create a logical vector named low that tells us which entries of murder_rate are lower than 1.

# Compute the murder rate per 100,000
murder_rate <- murders$total / murders$population * 100000

# Create a logical vector indicating which entries are lower than 1
low <- murder_rate < 1
print(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

##(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.

# Determine the indices of murder_rate associated with values lower than 1
indices_low_murder_rate <- which(low)

# Print the indices
print(indices_low_murder_rate)
##  [1] 12 13 16 20 24 30 35 38 42 45 46 51

##(3)Use the results from the previous exercise to report the names of the states with murder rates lower than 1.

# Get the names of states with murder rates lower than 1
states_low_murder_rate <- murders$state[indices_low_murder_rate]

# Print the names of states
print(states_low_murder_rate)
##  [1] "Hawaii"        "Idaho"         "Iowa"          "Maine"        
##  [5] "Minnesota"     "New Hampshire" "North Dakota"  "Oregon"       
##  [9] "South Dakota"  "Utah"          "Vermont"       "Wyoming"

##(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 &.

# Define the Northeast states
northeast_states <- c("Connecticut", "Maine", "Massachusetts", "New Hampshire", "New Jersey", "New York", "Pennsylvania", "Rhode Island", "Vermont")

# Create a logical vector indicating which states are in the Northeast and have low murder rates
northeast_low <- low & murders$state %in% northeast_states

# Get the names of states in the Northeast with murder rates lower than 1
states_northeast_low_murder_rate <- murders$state[northeast_low]

# Print the names of states
print(states_northeast_low_murder_rate)
## [1] "Maine"         "New Hampshire" "Vermont"

##(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?

# Compute the murder rate per 100,000 for each state
murder_rate <- murders$total / murders$population * 100000

# Compute the average murder rate
average_rate <- mean(murder_rate)

# Count the number of states below the average murder rate
states_below_average <- sum(murder_rate < average_rate)

# Print the number of states below the average
print(states_below_average)
## [1] 27

##(6) Use the match function to identify the states with abbreviations AK, MI, and IA. Hint: start bydefining an index of the entries of murders$abb that match the three abbreviations, then use the [operator to extract the states.

# Compute the murder rate per 100,000 for each state
murder_rate <- murders$total / murders$population * 100000

# Compute the average murder rate
average_rate <- mean(murder_rate)

# Count the number of states below the average murder rate
states_below_average <- sum(murder_rate < average_rate)

# Print the number of states below the average
print(states_below_average)
## [1] 27

##(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

# Define the abbreviations to check
abbreviations_to_check <- c("MA", "ME", "MI", "MO", "MU")

# Create a logical vector indicating which abbreviations are in the dataset
abbreviations_exist <- abbreviations_to_check %in% murders$abb

# Print the logical vector
print(abbreviations_exist)
## [1]  TRUE  TRUE  TRUE  TRUE FALSE

##(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 to check
abbreviations_to_check <- c("MA", "ME", "MI", "MO", "MU")

# Create a logical vector indicating which abbreviations are in the dataset
abbreviations_exist <- abbreviations_to_check %in% murders$abb

# Find the index of the entry that is not an actual abbreviation
index_not_abbreviation <- which(!abbreviations_exist)

# Get the non-matching abbreviation
non_matching_abbreviation <- abbreviations_to_check[!abbreviations_exist]

# Print the result
print(paste("The entry", non_matching_abbreviation, "at index", index_not_abbreviation, "is not an actual abbreviation."))
## [1] "The entry MU at index 5 is not an actual abbreviation."

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.