library(dslabs)
data("murders")

Exercise#3.11

1. Use the $ operator to access the population size data and store it as the object pop. Then use the sort function to redefine pop so that it is sorted. Finally, use the [ operator to report the smallest population size.

pop <- murders$population
pop <- sort(pop)
smallest_population <- pop[1]

2. Now instead of the smallest population size, fnd the index of the entry with the smallest populationsize. Hint: use order instead of sort.

index_smallest_population <- order(murders$population)[1]

3. We can actually perform the same operation as in the previous exercise using the function which.min.Write one line of code that does this.

index_smallest_population <- which.min(murders$population)

4. Now we know how small the smallest state is and we know which row represents it. Which state is it?Defne a variable states to be the state names from the murders data frame. Report the name of the state with the smallest population.

states <- murders$state
state_with_smallest_population <- states[index_smallest_population]

5. You can create a data frame using the data. frame function. Here is a quick example:

temp<- c(35, 88, 42, 84, 81, 30)

city<- c(“Beijing”, “Lagos”, “Paris”, “Rio de Janeiro”, “San Juan”, “Toronto”)

city_temps<- data.frame(name = city, temperature = temp)

Use the rank function to determine the population rank of each state from smallest population size to biggest. Save these ranks in an object called ranks, then create a data frame with the state name and its rank. Call the data frame my_df.

ranks <- rank(murders$population)
my_df <- data.frame(state = states, rank = ranks)

6. Repeat the previous exercise, but this time order my_df so that the states are ordered from leastpopulous to most populous. Hint: create an object ind that stores the indexes needed to order thepopulation values. Then use the bracket operator [ to re-order each column in the data frame.

ind <- order(ranks)
my_df <- my_df[ind, ]

7. The na_example vector represents a series of counts. You can quickly examine the object using:

data(“na_example”) str(na_example)

#> int [1:1000] 2 1 3 2 1 3 1 4 3 2 …

However, when we compute the average with the function mean, we obtain an NA: mean(na_example)

#> [1] NA

The is. na function returns a logical vector that tells us which entries are NA. Assign this logical vector ton object called ind and determine how many NAs na_example has.

data("na_example")
ind <- is.na(na_example)
number_of_nas <- sum(ind)

8. Now compute the average again, but only for the entries that are not NA. Hint: remember the! operator.

average_without_na <- mean(na_example[!ind])

Exercise#3.13

1. Previously we created this data frame:

temp <- c(35, 88, 42, 84, 81, 30)

city <- c(“Beijing”, “Lagos”, “Paris”, “Rio de Janeiro”, “San Juan”, “Toronto”)

city_temps <- data.frame(name = city, temperature = temp)

Remake the data frame using the code above, but add a line that converts the temperature from Fahrenheit to Celsius. The conversion is C = 5 9 × (F − 32).

temp <- c(35, 88, 42, 84, 81, 30)

city <- c("Beijing", "Lagos", "Paris", "Rio de Janeiro", "San Juan", "Toronto")

# Convert temperature from Fahrenheit to Celsius
temp_celsius <- (5/9) * (temp - 32)

city_temps <- data.frame(name = city, temperature = temp_celsius)

2. What is the following sum 1+1/22 + 1/32 + … 1/1002? Hint: thanks to Euler, we know it should be close to π2/6.

n <- 1000
sum_result <- 1

for (i in 2:n) {
  sum_result <- sum_result + 1 / i^2
}

result <- sum_result
cat("The sum is approximately:", result)
## The sum is approximately: 1.643935

3. Compute the per 100,000 murder rate for each state and store it in the object murder_rate. Then compute the average murder rate for the US using the function mean. What is the average?

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

# Compute the average murder rate for the US
us_average_murder_rate <- mean(murder_rate)

us_average_murder_rate
## [1] 2.779125