2 Activity 1
2.1 Dice activity
Take two dice and roll the pair of them 25 times, keeping track of the sum of their values. Fill those into the dice_rolls
variable below.
Exercise 2.1
- From the
sum_count
table, report how many times you rolled a 2, 3, 4, etc. After you have done that, change the last line fromsum_count
tobarplot(sum_count)
and run that to get a barplot. - Which sum (or sums) occurred the most number of times?
- Based on your data, are we more likely to roll a 2 or a 7?
- The function
mean()
returns the mean of a dataset. Run the next code box and report the mean of the data.
It is likely the case that in 25 rolls there were some totals you never rolled, if so, report those numbers as 0.
2.2 Data activity
R, and it’s many packages, contain many different datasets for practicing its features. One of these is the starwars
dataset in the dplyr
package. To access this, we first load the package with library("dplyr")
and then we can preview the dataset by simply writing starwars
.
While not strictly, necessary, I added suppressMessages()
to hide the messages that often come up when loading a package.
This is just a preview of the first 10 rows of data. Some of the columns aren’t displayed either. You can see the full dataset here.
If we want information about the starwars
dataset (or any R function), we can use the ?
operator.
Exercise 2.2
- What types of variables are included in this dataset? Which variables are quantitative?
- If we want to talk about a specific column, like
gender
, we can use the$
operator. Let’s use this now to create a pie chart of character gender in Star Wars. Make a quick sketch of the resulting chart.
- Try replacing
gender
by a different column likeheight
,sex
, orhomeworld
. For which of these does it make sense to use a pie chart? For which of these does a pie chart make sense but it would be better to group smaller categories into “other”?
2.3 Syntax and Functions
mean(...)
the mean or average of a list of numbers$
takes a specific column from a dataset e.g.starwars$height
?
put this before any function on built-in dataset to get more information about itpie(...)
creates a pie chartlibrary(...)
used to add additional features to R (i.e. new functions and new datasets).suppressMessages(...)
loads these in without printing any new messages.