library(learnr) library(learnSTATS) knitr::opts_chunk$set(echo = FALSE)
In this example, you will practice your R skills! You will map key concepts of variable types to objects types and begin interacting in a console as part of this tutorial. The learning objectives include:
The following videos are provided as a lecture for the class material. You can view the lecture notes within R using vignette("Introduction-to-R", "learnSTATS")
. You can skip these pages if you are in class to go on to the assignment.
In this next section, you will answer questions using the R code blocks provided. Be sure to use the solution
option to see the answer if you need it!
Please enter your name for submission. If you do not need to submit, just type anything you'd like in this box.
question_text( "Student Name:", answer("Your Name", correct = TRUE), incorrect = "Thanks!", try_again_button = "Modify your answer", allow_retry = TRUE )
Use logical operations to get R to agree that "two plus two equals 5" is FALSE.
2 + 2 == 5
Use logical operations to test whether 8 ^ 13 is less than 15 ^ 9.
8 ^ 13 < 15 ^ 9
Create a variable called potato
whose value corresponds to the number of potatoes
you've eaten in the last week. Then print out the value of potato
.
#there are a lot of right answers! potato <- 15
Calculate the square root of potato
using the sqrt()
function. Print out the value of potato again to verify that the value of potato hasn't changed. Note that potato
has been set to 15 for this question.
potato <- 15
sqrt(potato)
potato
Reassign the value of potato
to potato * 2
. Print out the new value of potato
to verify that it has changed.
potato <- 15
potato <- potato * 2 potato
Create three new variables:
charVar
. logicalVar
.NA
, and name that variable missingVar
. class()
to verify that each variable is the right type. charVar <- "cheese" logicalVar <- TRUE missingVar <- NA class(charVar) class(logicalVar) class(missingVar)
Create a numeric vector with three elements using c().
#lots of right answers! c(1,2,3)
Create a character vector with three elements using c().
#lots of right answers! c("this", "is", "characters")
Create a numeric vector called age
whose elements contain the ages of three people you know. Add the names of these people using names(age)
.
#lots of right answers! age <- c(34, 38, 27) names(age) <- c("scott", "abby", "katy")
Use "indexing by number" to get R to print out the first element of Orange$circumference
.
data("Orange")
data("Orange") Orange$circumference[1]
Use logical indexing to return all the circumferences of Orange trees greater than 100.
Orange$circumference[]
Orange$circumference[Orange$circumference > 100]
Load the airquality
dataset. Use the $ method to print out the Wind
variable in airquality.
data("airquality") airquality$Wind
Print out only the first 10 cases (rows) in the airquality
dataset. Hint: typing c(1,2,3,4,5,6,7,8,9,10) is tedious. R allows you to use 1:10 as a shorthand method!
airquality[1:10 , ]
Use logical indexing to print out all days in airquality
where the Ozone
level was higher than 20.
airquality[airquality$Ozone > 20 , ]
Use subset() to do the same thing. Notice the difference in the output.
subset(airquality, Ozone > 20)
Use the length()
function to determine the number of observations in the airquality
dataframe.
#remember that number of rows needs to be on a vector length(airquality$Ozone)
Make a simple function to calculate x + 6.
addup <- function(x) { x + 6 }
Load the utils
library.
library(utils)
On this page, you will create the submission for your instructor (if necessary). Please copy this report and submit using a Word document or paste into the text window of your submission page. Click "Generate Submission" to get your work!
encoder_logic()
encoder_ui()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.