Changing working directories
After running this command, all data input and output will default to the
mydata folder in the C: drive.
If you are accustomed to folder names in Windows, you might have
expected this to be written as "c:\mydata".
However, R treats the backslash character “\” as a special “escape”
character, which modifies the interpretation of the next character. If you
really want a backslash, you need to double it: the first backslash tells
the second backslash not to be an escape.
Because other systems use a forward slash “/” in their folder names,
and because doubling the backslash is tedious in Windows, R accepts
either form.
2
dump() and source()
Suppose you have constructed an R object called usefuldata. In order
to save this object for a future session, type
dump("usefuldata", "useful.R")
This stores the command necessary to create the vector usefuldata
into the file useful.R on your computer’s hard drive.
3
dump() and source()
The choice of filename is up to you, as long as it conforms to the usual
requirements for filenames on your computer.
To retrieve the vector in a future session, type
source("useful.R")
This reads and executes the command in useful.R, resulting in the
creation of the usefuldata object in your global environment. If there
was an object of the same name there before, it will be replaced.
To save all of the objects that you have created during a session, type
dump(list = objects(), "all.R")
This produces a file called all.R on your computer’s hard drive. Using
source("all.R") at a later time will allow you to retrieve all of these
objects.
4
Example
To save existing objects nhtemp and nhtempC to a file called nhtemp.R
on your hard drive, type
dump(c("nhtemp", "nhtempC"), "nhtemp.R")
5
Saving and retrieving image files
The vectors and other objects created during an R session are stored in
the workspace known as the global environment.
When ending an R session, we have the option of saving the workspace
in a file called a workspace image.
If we choose to do so, a file called by default .RData is created in the
current working directory (folder) which contains the information
needed to reconstruct this workspace.
6
Saving and retrieving image files
In Windows, the workspace image will be automatically loaded if R is
started by clicking on the icon representing the file .RData, or if the
.RData file is saved in the directory from which R is started.
If R is started in another directory, the load() function may be used to
load the workspace image.
7
Saving and retrieving image files
It is also possible to save workspace images without quitting. For
example, we could save all current workspace image information to a file
called temp.RData by typing
save.image("temp.RData")
Again, we can begin an R session with that workspace image, by
clicking on the icon for temp.RData. Alternatively, we can type
load("temp.RData") after entering an R session. Objects that were
already in the current workspace image will remain, unless they have the
same name as objects in the workspace image associated with
temp.RData. In the latter case, the current objects will be overwritten
and lost.
8
Vectors in R
Data come in the form of numbers and characters.
In R, a vector is a list of either numbers or of characters.
We will see some other kinds of vectors as well.
9
Numeric Vectors
A numeric vector is a list of numbers.
The rivers object is an example of a vector that is built in to R.
We can count the number of elements in rivers using the length()
function:
length(rivers)
## [1] 141
This vector contains 141 elements.
10
Numeric Vectors
Last lecture, we learned that we can view the entire contents of an object
by typing its name. Let’s do that one more time:
rivers
## [1] 735 320 325 392 524 450 1459 135 465 600 330
## [12] 336 280 315 870 906 202 329 290 1000 600 505
## [23] 1450 840 1243 890 350 407 286 280 525 720 390
## [34] 250 327 230 265 850 210 630 260 230 360 730
## [45] 600 306 390 420 291 710 340 217 281 352 259
## [56] 250 470 680 570 350 300 560 900 625 332 2348
## [67] 1171 3710 2315 2533 780 280 410 460 260 255 431
## [78] 350 760 618 338 981 1306 500 696 605 250 411
## [89] 1054 735 233 435 490 310 460 383 375 1270 545
## [100] 445 1885 380 300 380 377 425 276 210 800 420
## [111] 350 360 538 1100 1205 314 237 610 360 540 1038
## [122] 424 310 300 444 301 268 620 215 652 900 525
## [133] 246 360 529 500 720 270 430 671 1770
11
Extracting elements from vectors
Suppose we want to see the 35th measurement in the rivers object.
You can extract this element from the rivers vector using the value 35
inside square brackets:
rivers[35]
## [1] 327
12
Extracting elements from vectors: Example
nhtemp contains average annual temperatures for New Haven,
Connecticut, starting in 1912. We can extract the 5th element of this time
series vector using
nhtemp[5]
## [1] 49.4
This gives us the average temperature for 1916.
13
Polling question - Yes or No?
How do we find the number of elements in nhtemp, i.e. the length of
nhtemp?
(a) length(nhtemp)
(b) length[nhtemp]
Is the correct answer (a)?
14
Polling question - Answer
Yes!
length(nhtemp)
## [1] 60
There are 60 elements in the nhtemp vector.
15
Polling question - Yes or No?
How do we find the 57th element of nhtemp?
(a) nhtemp(57)
(b) nhtemp[57]
Is the correct answer (a)?
16
Polling question - Answer
No!
nhtemp(57)
## Error in nhtemp(57): could not find function "nhtemp"
You should use the square brackets, not the round brackets, when
extracting an element from a vector. Here, R is telling you that nhtemp is
not a function - which means that it thinks you want to use nhtemp to be
a function. (You don’t)
The 57th element of nhtemp is correctly displayed with
nhtemp[57]
## [1] 51.9
17
Building your own numeric vectors
The c() function is used to collect things together into a vector. We can
create a vector called myvector which contains some random data:
myvector <- c(2.5, 5, 0, 0.7, -8)
We can see the contents of myvector by typing its name
myvector
## [1] 2.5 5.0 0.0 0.7 -8.0
18
Polling question: Yes or no?
I want to assign the first 3 prime numbers to the object prime3. Does
the following work?
prime3 <- (2, 3, 5)
19
Polling question: Answer
No! You need to use the c function:
prime3 <- c(2, 3, 5)
20
Vectors of Sequences
Earlier, we learned that the : symbol can be used to create sequences of
increasing (or decreasing) values.
We can create a vector called numbers5to20 which contains all of the
integers from 5 through 20:
numbers5to20 <- 5:20
numbers5to20
## [1] 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21
Putting Vectors Together
Vectors can be joined together (i.e. concatenated) with the c function.
For example, watch what happens when we combine the existing object
numbers5to20 with the numbers 31 through 35:
c(numbers5to20, 31:35)
## [1] 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 31 32
## [19] 33 34 35
22
Putting Vectors Together
Here is another example of the use of the c() function.
some.numbers <- c(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
41, 43, 47, 59, 67, 71, 73, 79, 83, 89, 97, 103, 107, 109,
113, 119)
If you type this in the R console (not in the RStudio Source Pane), R will
prompt you with a + sign for the second line of input. RStudio doesn’t
add the prompt, but it will indent the second line.
In both cases you are being told that the first line is incomplete: you
have an open bracket which must be followed by a closing bracket in
order to complete the command.
Also, don’t forget to include all the commas where they are needed.*
* Watch the video vectorErrorMsg.mp4 for an example.
23