This is just a suggestion to try and help get you into "good habits" early. If I was sitting to take this practical now, I would start a new R script file. That way all of the work that you have done associated with today's course is in one place, and the code for each of the practicals is separate from one another. This might feel a bit tedious right now, but as the amount of code you write and number of projects you take part in increases it will pay off to have a structured workflow.
For this set of questions we will use the movies data from the IMDB database. Run the line of code below to load the data set into your R session.
data(movies, package = "jrIntroduction")
Use the head()
function to inspect the top of the data, this can help give you a feel for what the data looks like and what variables are contained within the data
r
head(movies)
How many films and how many variables are in this data set?
r
dim(movies)
Recall that if I want to pull out a single column, or variable, from a data frame we can use $
. To extract the titles from this data set we write movies$title
.^[If you can't remember what the names of the columns are, you can use colnames(movies)
to find out.] Using mean()
and median()
calculate these summary statistics for the film lengths
r
mean(movies$duration)
median(movies$duration)
What year is the oldest film in the data set from?
r
min(movies$year)
How long is the longest film?
r
max(movies$duration)
What is the standard deviation of the film ratings?
r
sd(movies$rating)
Try running the following code
r
table(movies$action)
What do you think is happening?
r
# Counting up the categories within a vector
To give you some practice at reading in your own data, we're going to get you to read it in. The function
library("jrIntroduction") get_csv_movies_file()
\noindent will copy a file called movies.csv
into your current working directory. You can now
import the data set using the Import Dataset button in RStudio, under the
Environment tab
fname = system.file("import_data.png", package = "jrIntroduction") knitr::include_graphics(fname)
\noindent This will generate R code that you can reuse.
Solutions to the practical questions are contained within the package
vignette("solutions2", package = "jrIntroduction")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.