paste0() ?
Module 1: An Introduction and Motivation for R Programming
We will spend time motivating why R is such a commonly used and powerful language in statistical programming and how it will be useful for them in their future endeavors (public policy, economics, data science applications, etc.). We will go through the interface of R Studio to better understand the environment and learn how to do simple variable assignment, arithmetic, and logical comparison.
An Introduction and Motivation for R Programming
Download a copy of Module 1 slides
Lab 1
We expect you to watch the Module 1 material prior to lab.
General Guidelines:
You will encounter a few functions we did not cover in the lecture video. This will give you some practice on how to use a new function for the first time. You can try following steps:
- Start by typing
?new_function
in your Console to open up the help page - Read the help page of this
new_function
. The description might be too technical for now. That’s OK. Pay attention to the Usage and Arguments, especially the argumentx
orx
,y
(when two arguments are required) - At the bottom of the help page, there are a few examples. Run the first few lines to see how it works
- Apply it in your lab questions
It is highly likely that you will encounter error messages while doing this lab Here are a few steps that might help get you through it.
- Locate which line is causing this error first
- Check if you may have a typo in the code. Sometimes another person can spot a typo faster than you.
- If you enter the code without any typo, try googling the error message
- Scroll through the top few links see if any of them helps
Warm-up
The most important warm up question: do you have R and RStudio installed?
Which of these allow you to pull up the documentation for a command in R?
*
?
help()
documentation()
- In the code block below, run code that will pull up documentation for the function
paste0()
.
What does this function do?
- What are the two ways that you can assign a value to a variable?
Guess the Output: Algebra
- Guess the output of the following code:
<- 3
a <- a^2 + 1
b
b
Now, run the code block to check your answer.
- Guess the output of the following code:
<- 10
a <-3 %% a
b
+ 5 b
Hint: If you are not sure what %%
does you can try running ?'%%'
to better understand.
- Guess the output of the following code:
<- c(1,2,3)
a <- a^2 + 1
b
b
Guess the Output: Boolean
- Guess the output of the following code:
25 >= 14
- Guess the output of the following code:
10 != 100
- Guess the output of the following code:
7%%5 == 2
- Guess the output of the following code:
5 > 7) & (7 * 7 == 49) (
- Ok, let’s try some logic! Try to figure out each one before running the code!
TRUE & FALSE
FALSE & FALSE
TRUE | (FALSE & TRUE)
FALSE | (TRUE | FALSE)
TRUE & (TRUE | FALSE)) | FALSE (
Data Types
- Run these lines to create these variables in your environment.
<- "Hi, my name is item 1!"
item_1 <- 7
item_2 <- FALSE item_3
What are the type (or mode) of each of these items?
Hint: If you are not sure, you could apply the mode()
function to each item and check the output. If you are unsure about how to apply the mode()
function, you can always run ?mode()
.
- Guess the output of the following code:
+ 19 <= 25) == item_3 (item_2
- Do you remember earlier when you ran
?paste0()
? We are now going to try to use this function. In the code block below, initialize two variables that are of mode “character”. The output when you applypaste0()
to these variables should be “Hello, world!”.
#v1 <-
#v2 <-
Well done! You’ve learned how to work with R to perform simple variable assignment and operations!
Want to improve this tutorial? Report any suggestions/bugs/improvements on here! We’re interested in learning from you how we can make this tutorial better.