Practice Set: Objects
Visualizing Star Wars
Now that you’re oriented to RStudio, it’s time to write and execute some code. The following uses a data set about Star Wars characters (yay!). This is the same dataset as the Objects lecture
You try:
- Read through the lines of code. You probably won’t understand anything, but that’s OK!
- Click “Run Code”
- Watch what happens!
Over the course of this basic training, we’ll cover all the elements in this code block that produced this graph. It’s 100% OK if it looks like complete gibberish to you right now!
Practice with Objects
- Objects allow you to store things (values, models, entire datasets etc.)
- They are the nouns of programming languages; they are typically what we are acting on
- We give an object a name; this is called assignment, and it takes the form of:
The thing on the LEFT is the name you want to give your object.
Then the arrow; first < (less than sign), then - (hyphen/dash) with no space in between.
The thing you want to actually store for later goes on the RIGHT.
You try! Create an object called continents that stores the number 7
Note:
- Make sure you hit “Run Code” before “Submit Answer”
- If you get a message that’s something like “Last value is invisible”, you can ignore it
Things to note:
If you were to run this line of code from within RStudio, you would see the word
continentsin the Environment pane (upper right side). This is an easy way for you to glance over and see if your object is actually stored.Notice that nothing printed out when you ran the code above. That code simply says “store the number
7in an object calledcontinents”. No where does it say “show me what is stored in thecontinentsobject. To do that, you simply need to type it out again, like this:
continents <- 7
continents
## [1] 7
☝️ The second line prints out continents, so the number 7 appears.
In your research, most of the time, you don’t necessarily want to print out every object. But it’s very helpful for things like homework assignments or showing your collaborators an answer/value.
Quick Quiz
The whole point of making an object is that you can do things with them!
For example:
# I have 5 chickens
chickens <- 5
# I have 4 cows
cows <- 4
# How many animals do I have in total?
chickens + cows
## [1] 9
You can also update your objects!
# I sold 3 chickens
chickens <- chickens - 3
# I bought 8 cows
cows <- cows + 8
# What is my new total number of animals?
# Still the same line of code from before!
chickens + cows
## [1] 14
In this scenario, we did an “overwrite” of our original chickens and cows objects. We could have stored those as new objects. Something like: chickens_new <- chickens - 3. You will need to decide whether it makes sense to overwrite an object or to create a new one. Base it on what you’re trying to accomplish.
Quick Quiz
Practice with Vectors
A vector is a group of objects that’s 1-dimensional:
- It can be a row, a column, or just stand alone
- To make a vector, you can combine or concatenate objects together using
c() - Each object (vector or otherwise) has a specific data class. this can be things like numeric, character, factors, logical etc.
Check out this table:
| ID | State | Smoker | Age |
|---|---|---|---|
| ID1 | Missouri | TRUE | 20 |
| ID2 | Iowa | FALSE | 18 |
| ID3 | Missouri | FALSE | 32 |
| ID4 | Idaho | TRUE | 25 |
| ID5 | Maine | FALSE | 25 |
Here, each row can be considered it’s own vector, and each column can be considered it’s own vector.
You try:
- First, create 4 vectors for each of these columns. Make sure they contain the same exact values as what you see in this table! Assign each vector to the following corresponding name:
idsstatesmokerage
- Then, check the
class()of each of your newly created vectors.
Remember Star Wars?
You’ve already made a factor! You turned species into a factor. The result is that in our graph, we have discrete categories for the different types of Star Wars characters!
You are a ⭐
Learning to code can be really intimidating. But you’re doing great! Keep it up!
