For this lab, you will create a single .R
file called lab02.R
. The following exercises will ask you to write code. Place all requested code in this .R
file separated by comments which indicate which code corresponds to which exercise.
Submit your lab to the corresponding assignment on Canvas. You have unlimited attempts before the deadline. Your final submission before the deadline will be graded.
This lab will largely be graded based on completion. For each exercise that you demonstrate a good-faith effort to complete, you will receive two points.
In this exercise we will create, but not store, several vectors. Write code that creates the following vectors:
c()
function.42
, 42
, and 3.14
. Give the elements names one
, two
, and three
respectively.TRUE
, TRUE
, FALSE
repeated 10 times.Place this code in your .R
file for this exercise. Also, run your code to make sure it does what you expect and does not produce any errors!
Create a matrix named c_matrix
that stores the integers from 1
to 100
arranged by column. (That is, the first column will contain 1
, 2
, 3
, etc.) The matrix should have 25 rows and 4 columns.
Also create a matrix named r_matrix
that does the same thing, but arranged by row. It should also have 25 rows and 4 columns.
matrix()
function. Use ?matrix
to read the documentation.Run and include the following code in your .R
file.
c_matrix
r_matrix
Create but do not store a list containing the following elements:
x
which contains only the numeric (double or integer) value 42.y
which has 5 rows and 2 columns. It can contain any values.z
that contains the months of the year, that is January
, February
, etc. Hint: Run ?letters
.Create the following data frame with the name starter_pokemon
:
pokedex_num name type_primary type_secondary
1 1 Bulbasaur Grass Poison
2 2 Ivysaur Grass Poison
3 3 Venusaur Grass Poison
4 4 Charmander Fire
5 5 Charmeleon Fire
6 6 Charizard Fire
7 7 Squirtle Water
8 8 Wartortle Water
9 9 Blastoise Water
After creating the data frame, run the code:
starter_pokemon
Also include that code in your .R
file.
Some notes:
pokedex_num
vector is integers.type_secondary
vector can be created with ""
.The previous exercise is not how things work in the real world. In this exercise, you will run some code to import external data. You don’t need to write any code yourself. However, run the following code to see what it does! Also include it in your .R
file.
pokemon = read.csv("https://stat385.org/data/pokemon.csv")
head(pokemon)
tail(pokemon)
pokemon[1:25, 1:4]
pokemon[pokemon$legendary == TRUE, ]
We’ll talk about what some of this code does later, but see if you can already figure out what it does.