Exercise : 3.11
Loading Murder Dataset from library ‘dslabs’
library(dslabs)
data(murders)
Question#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_size<-pop[1]
cat("Smallest_population_size is :",smallest_population_size)
## Smallest_population_size is : 563626
Question#2:
Now instead of the smallest population size, find the index of the entry with the smallest population size. Hint: use order instead of sort.
pop<-murders$population
smallest_population<-order(pop)[1]
cat("smallest_population_size =", smallest_population)
## smallest_population_size = 51
Question#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<-which.min(murders$population)
index_smallest
## [1] 51
Question#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.
ind<-which.min(murders$population)
state_min_pop<-murders$state[ind]
state_min_pop
## [1] "Wyoming"
Question#5:
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)
ranks
## [1] 29 5 36 20 51 30 23 7 2 49 44 12 13 47 37 22 19 26 27 11 33 38 43 31 21
## [26] 34 8 14 17 10 41 16 48 42 4 45 24 25 46 9 28 6 35 50 18 3 40 39 15 32
## [51] 1
my_df<-data.frame(state=murders$state , rank=ranks)
my_df
## state rank
## 1 Alabama 29
## 2 Alaska 5
## 3 Arizona 36
## 4 Arkansas 20
## 5 California 51
## 6 Colorado 30
## 7 Connecticut 23
## 8 Delaware 7
## 9 District of Columbia 2
## 10 Florida 49
## 11 Georgia 44
## 12 Hawaii 12
## 13 Idaho 13
## 14 Illinois 47
## 15 Indiana 37
## 16 Iowa 22
## 17 Kansas 19
## 18 Kentucky 26
## 19 Louisiana 27
## 20 Maine 11
## 21 Maryland 33
## 22 Massachusetts 38
## 23 Michigan 43
## 24 Minnesota 31
## 25 Mississippi 21
## 26 Missouri 34
## 27 Montana 8
## 28 Nebraska 14
## 29 Nevada 17
## 30 New Hampshire 10
## 31 New Jersey 41
## 32 New Mexico 16
## 33 New York 48
## 34 North Carolina 42
## 35 North Dakota 4
## 36 Ohio 45
## 37 Oklahoma 24
## 38 Oregon 25
## 39 Pennsylvania 46
## 40 Rhode Island 9
## 41 South Carolina 28
## 42 South Dakota 6
## 43 Tennessee 35
## 44 Texas 50
## 45 Utah 18
## 46 Vermont 3
## 47 Virginia 40
## 48 Washington 39
## 49 West Virginia 15
## 50 Wisconsin 32
## 51 Wyoming 1
Question#6:
Repeat the previous exercise, but this time order my_df so that the states are ordered from least populous to most populous. Hint: create an object ind that stores the indexes needed to order the population values. Then use the bracket operator [ to re-order each column in the data frame.
indexed_rank <- order(my_df$rank)
my_df<-my_df[indexed_rank, ]
my_df
## state rank
## 51 Wyoming 1
## 9 District of Columbia 2
## 46 Vermont 3
## 35 North Dakota 4
## 2 Alaska 5
## 42 South Dakota 6
## 8 Delaware 7
## 27 Montana 8
## 40 Rhode Island 9
## 30 New Hampshire 10
## 20 Maine 11
## 12 Hawaii 12
## 13 Idaho 13
## 28 Nebraska 14
## 49 West Virginia 15
## 32 New Mexico 16
## 29 Nevada 17
## 45 Utah 18
## 17 Kansas 19
## 4 Arkansas 20
## 25 Mississippi 21
## 16 Iowa 22
## 7 Connecticut 23
## 37 Oklahoma 24
## 38 Oregon 25
## 18 Kentucky 26
## 19 Louisiana 27
## 41 South Carolina 28
## 1 Alabama 29
## 6 Colorado 30
## 24 Minnesota 31
## 50 Wisconsin 32
## 21 Maryland 33
## 26 Missouri 34
## 43 Tennessee 35
## 3 Arizona 36
## 15 Indiana 37
## 22 Massachusetts 38
## 48 Washington 39
## 47 Virginia 40
## 31 New Jersey 41
## 34 North Carolina 42
## 23 Michigan 43
## 11 Georgia 44
## 36 Ohio 45
## 39 Pennsylvania 46
## 14 Illinois 47
## 33 New York 48
## 10 Florida 49
## 44 Texas 50
## 5 California 51
Question#7:
The is.na function returns a logical vector that tells us which entries are NA. Assign this logical vector to an object called ind and determine how many NAs does na_example have.
data("na_example")
str(na_example)
## int [1:1000] 2 1 3 2 1 3 1 4 3 2 ...
ind <-is.na(na_example)
num_of_NAs <- sum(ind)
cat("The number of NAs in vector is :", num_of_NAs)
## The number of NAs in vector is : 145
Question#8:
Now compute the average again, but only for the entries that are not NA. Hint: remember the ! operator.
avg_of_non_NA <- mean(na_example[!is.na(na_example)])
avg_of_non_NA
## [1] 2.301754
Exercise: 3.13
Question#1:
Remake the data frame using the code above, but add a line that converts the temperature from Fahrenheit to Celsius. The conversion is C = 59 × (F − 32).
temp <- c(35, 88, 42, 84, 81, 30)
city <- c("Beijing", "Lagos", "Paris", "Rio de Janeiro", "San Juan", "Toronto")
city_temps <- my_dataframe<-data.frame(name = city, temperature = temp)
celsius_temp<-(temp-32)*5/9
cat("by converting farenhite to celsius:\n",celsius_temp)
## by converting farenhite to celsius:
## 1.666667 31.11111 5.555556 28.88889 27.22222 -1.111111
Question#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.
terms <- 1 / (1:1000)*2
sum_result <- sum(terms)
cat("The sum is approximately:", sum_result, "\n")
## The sum is approximately: 14.97094
Question#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?
murder_rate <- (murders$total/ murders$population) * 100000
avg_murder_rate<-mean(murder_rate)
cat("avg_murder_rate for US is : ",avg_murder_rate)
## avg_murder_rate for US is : 2.779125