{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)
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:
{r} library(dslabs)
You can also embed plots, for example:
{r} data(murders) #q1: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.
{r} pop<-murders$population pop
{r} sort(pop)
{r} murders$population[which.min(pop)]
{r} min(murders$population) #q2:Now instead of the smallest
population size, find the index of the entry with the smallest
population size. {r} order(murders$population) #q3: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.
{r} murders$population[which.min(pop)] #q4:Now we know how
small the smallest state is and we know which row represents it. Which
state is it? Define a variable states to be the state names from the
murders data frame. Report the name of the state with the smallest
population. {r} state<-murders$state state
{r} murders$state[which.min(murders$population)] #q5:You
can create a data frame using the data.frame function. ```{r}
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) city_temps
```{r}
population<-murders$population
population
{r} states<-murders$state states
{r} ranks <- rank(murders$population) ranks
```{r}
my_df <- data.frame(name=state, ranks) my_df
#q6: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.
```{r}
state<-(murders$population)
state
{r} rank<-rank(murders$population) rank
{r} ind<-order(murders$population) ind
{r} my_df<-data.frame(state=state[ind],rank=rank[ind]) my_df
#q7:he na_example vector represents a series of counts
{r} data("na_example") str(na_example) #However, when we
compute the average with the function mean, we obtain an NA:
{r} mean(na_example)
{r} index<-is.na(na_example) index
{r} num_NAs<-sum(ind) num_NAs #q8:Now compute the
average again, but only for the entries that are not NA. Hint: remember
the !operator. {r} mean(na_example[!ind]) #q9:Previously we
created this data frame:
{r} 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) city_temps
{r} temp<-5/9*(temp-32) temp #q10: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. {r} x<-c(1:1000) x sum(1/x^2)
#11:ompute 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?
{r} murder_rate<-murders$total/murders$population *100000 murder_rate
{r} mean(murder_rate) Note that the
echo = FALSE parameter was added to the code chunk to
prevent printing of the R code that generated the plot.