1. Create a new R script (File \(\rightarrow\) New File \(\rightarrow\) R Script). Save it somewhere as lecture_intro.R
.
2. Type the following code in your script and run it. To run the code press Ctrl
or Cmd
+ Enter
(you can either highlight the code or just put your cursor at the end of the line)
4 * 8
## [1] 32
3. Type the following code in your script and run it. What happens if you only run the first line of the code?
x = 5 # equivalently x <- 5
x
## [1] 5
x = 5
If I only run the first line of code, the object x
is created in my environment but no output appears in the console. This is because I am not asking R
to output anything; the only thing I am asking it is to create an object x
equal to \(5\).
Congratulations, you have created your first R
“object”! Everything is an object in R! Objects are assigned using =
or <-
.
4. Create a new object named x_3
to which you assign the cube of x
. Note that to assign you need to use =
or <-
. Use code to compute the cube, not a calculator.
x_3 = x^3
x_3
## [1] 125
1. Find out (using help()
or google) how to import a .csv file. Do NOT use the “Import Dataset” button, nor install a package.
There are many ways to import a .csv file. The simplest way is to use the read.csv()
function.
2. Import gun_murders.csv1 in a new object murders
. This file contains data on gun murders by US state in 2010. (Hint: objects are created using =
or <-
).
link <- "https://www.dropbox.com/s/zuk0qcfm3kyzs4e/gun_murders.csv?dl=1"
murders <- read.csv(link)
3. Ensure that murders
is a data.frame by running:
class(murder)
## Error in eval(expr, envir, enclos): object 'murder' not found
Hmm I get an error: “object ‘murder’ not found.” Why is that? It’s simple, there’s a typo: it should be murders
not murder
. Everytime you see an error that says objet x
not found it simply means that there is no object named x
in your environment. So either you haven’t created it yet or there’s a small typo in your code. Always read what the error message says!
class(murders)
## [1] "data.frame"
4. Find out what variables are contained in murders
by running
names(murders)
## [1] "state" "abb" "region" "population" "total"
5. View the contents of murders
by clicking on murders
in your workspace. What does the total
variable correspond to?
View(murders)
The total
variable corresponds to the total number of gun murders by state (in 2010).
1. How many observations are there in murders
?
nrow(murders)
## [1] 51
2. How many variables? What are the data types of each variable?
str(murders)
## 'data.frame': 51 obs. of 5 variables:
## $ state : chr "Alabama" "Alaska" "Arizona" "Arkansas" ...
## $ abb : chr "AL" "AK" "AZ" "AR" ...
## $ region : chr "South" "West" "West" "South" ...
## $ population: int 4779736 710231 6392017 2915918 37253956 5029196 3574097 897934 601723 19687653 ...
## $ total : int 135 19 232 93 1257 65 97 38 99 669 ...
3. Remember that the colon operator 1:10
is just short for construct a sequence from 1
to 10
(i.e. 1, 2, 3, etc). Create a new object murders_2
containing the rows 10 to 25 of murders
.
murders_2 <- murders[10:25,]
nrow(murders_2)
## [1] 16
4. Create a new object murders_3
which only contains the columns state
and total
. (Recall that c
creates vectors.)
murders_3 <- murders[, c("state", "total")]
5. Create a total_percap
variable equal to the number of murders per 10,000 inhabitants by running the following code.
murders$total_percap = (murders$total / murders$population) * 10000
names(murders)
## [1] "state" "abb" "region" "population" "total"
## [6] "total_percap"
Congratulations, you’ve created your first variable! Click on the murders
object to see the new variable.