程序代写代做 go In-Class Activity # 5

In-Class Activity # 5
your_name_here date_here
For this activity, you will edit the provided R markdown document, knit it to a PDF, and upload the completed .Rmd file and PDF to wolfware. Be sure that your file has the appropriate name and date filled in. The purpose of this activity is to get practice with logicals and packages, and to build up your R markdown capabilities. To answer the questions in this activity, use code chunks where computation is needed and text where explanations are needed.
Packages
1) You can see what packages are installed on your computer using installed.packages(), and look for a specific one using “package” %in% rownames(installed.packages()). In your console, check if you have the tidyverse package installed – if you don¡¯t, install it now. Then include the check for tidyverse in a code chunk.
2) You can check the version of an installed package with the packageVersion function. In a code chunk, what version of the base package do you have?
3) In a code chunk, use :: to call any function from an unloaded package. Explain why you might want to call a function in this way rather than loading a package.
4) In a code chunk, load the tidyverse package using the library function and allow the loading messages to be shown in your PDF output. How many other packages are loaded when you do this?
Logicals
logical operators
1) Draw a vector of 20 random uniform(0,1) values and call it unif20. Output a check if the values of unif20 are greater than 0.5. (Note: this will change each time you knit your document or run it in the console/notebook)
2) Use the sum function to output the number of values in unif20 that are greater than 0.5. Use the mean function to output the fraction of values in unif20 are greater than 0.5. Explain what is happening to the logical TRUE/FALSE values that allows this to work?
3) The ! operator can also negate a logical statement – e.g. !TRUE returns FALSE. It is helpful to use it with parentheses to think about the order of operations, e.g. !(3>2). Redo problem 2) with the negation of the inequality.
logicals for subsetting data
1) Use [] notation to subset the iris dataset to the rows that have petal width between 2 and 3 and store as an object called iris23. Use an R function to output the number of rows in iris23.
2) Use the filter function from the dplyr package, without loading the package, to reduce iris to the rows that are not from the setosa species. Save this as an object called no_setosa.
3) Remove the Petal.Length column from no_setosa by overwriting it with a NULL. Print the resulting object to the console.
4) Use the subset function from base R to subset the rows where the species is ¡®versicolor¡¯ OR ¡®virginica¡¯, call this no_setosa2. With the nrow function and the == operator, check that the number of rows in no_setosa matches no_setosa2.
1

5) The which function can be used with logical operators to find the indices of an object that meet some condition. For example,
## [1] FALSE FALSE TRUE TRUE TRUE TRUE
## [1] 3 4 5 6
Applying which to a column of a dataframe will return the row indices that meet your condition. Use it to
print the row numbers of the ¡®Sepal.Width¡¯ column of iris that are equal to 3.
6) Recall that you can add to a named list by specifying list$new_element <-. If you are dealing with a data frame and give a vector of an appropriate length this will create a new column. Overwrite the iris object so that it has two new columns: Petal.Ratio and Sepal.Ratio which take the lengths and divide by the widths. 7) Print to the console the rows for which the petal ratio is greater than 3.3 and the sepal ratio is greater than 2. End of Activity Go back through and check that you have completed all questions. Change the Rmd file name to ¡®uni- tyID_activity05.Rmd¡¯, with your unity ID filled in. Then knit to PDF and upload both files to Moodle under In-Class Activity # 5. s <- seq(0, 10, by=2) ## vector of logicals s>3
## indices where condition is TRUE which(s > 3)
2