Hi! I am Mike Marin
and in this video we'll talk about creating vectors
and matrices in R and performing some simple operations on these
Recall that we can assign a value
to an object in R using the equal sign (=) or by creating an arrow.
Here we will assign the value 11 to the object x
Often we would like to create a vector
or column of numbers or characters. We can create a vector
in R using the "c" or "concatenate command"
let's create an object called x1
and we are storing this in a vector
of values 1, 3, 5, 7 and 9
here we can see these are now stored in the object x1
We can also create
a vector of character elements by including quotations
around the characters; for example let's create an object
gender and we will store in it the elements
"male" and the element "female"
we can create a sequence of
integer values using the colon (:), for example
creating a sequence running from 2 up to 7
taking on only integer values. for more general sequences
we can use the "seq" command;
here let's create a sequence running from 1
to 7 in increments of 1
if we would like a sequence
of non-integer values,
say a sequence running from 1 to 7 in increments of a third
or a sequence running from 1 up to 7 in increments of 0.25
we can use the "rep" command
to create a vector of repeated numbers or characters
let's create a vector repeating the number 1
and let's repeat this 10 times, we may also
create a vector of repeated characters
Here let's repeat the characters "marin" and let's repeat this
5 times; we may want to have
a sequence repeated multiple times, for example
we may want to repeat the sequence of numbers 1 to 3
and have this repeated 5 times
or we may want to repeat a sequence of numbers
for which non-integer values are used
for example a sequence running from 2 up to
5 in increments of 0.25
and let's have this repeated 5 times
we may also
want to have a sequence of characters repeated multiple times
for example we may want to repeat the characters
"m" and the character "f" and have this repeated
say 5 times, let's go ahead
and create a vector called x with
sequence of numbers running from 1 to 5 and
let's also create a vector called y with the numbers 1, 3, 5, 7 and 9
We will demonstrate a few things on
x & y in a moment
First we may add a value to each element of the vector
using the "plus command"; for example we may want to take
the object x or the vector x and add 10
to every element in this vector; similarly
we can subtract 10 from every element in this vector
we can also multiply each
element of this vector by a constant or
divide each element of this vector by a constant
if two vectors
are of
the same length we may
add, subtract, multiply or divide corresponding elements
Recall we had the vector
x and the vector y
since these two vectors are of the same length we may add them
in an element-wise fashion, we can subtract the two
again in an element-wise fashion, we can multiply
corresponding elements or we can divide
the corresponding elements. It is worth noting here
that slightly different commands are used in order to perform
vector or matrix multiplication in a linear algebra sense
we may extract elements of a vector using square brackets [ ]
recall the vector x
and the vector y
here we will extract some elements from the vector y
if we'd like to extract the third element in this vector y
we can place a 3 within the square brackets
here we can see we've extracted the third element which is the number 5
including a negative sign (-) will extract
all elements except the third element
we may wish to take y and extract the first three elements
we may wish to extract the first
and the fifth element or
we may wish to extract all elements except the first
and fifth
Finally, we may wish to take y and extract only the elements
where y is less than 6
that is for the vector y only extract the elements that are less than 6
we can create a matrix of values using the "matrix" command
let's go ahead and construct a matrix and here
we'll have the values 1,2,3,4,5,6,7,8,9
we will set the "nrow" argument equal to 3
letting R know we like a matrix with three rows and hence
three columns and we can set the "byrow" argument
equal to TRUE, note that this must be capital letters
to let R know to enter these elements row-wise
we can see if we set the "byrow" argument equal to FALSE
these elements will be entered in a column-wise fashion
let's go ahead and store this matrix
in an object called "mat"
we can use the square brackets in a similar fashion
to extract certain elements from this matrix
for example if we'd like to extract the element
in the first row and the second column.
we can also extract multiple elements from this matrix
for example we may want to extract
elements in row 1 and row 3
and column 2
Leaving a row or a column blank or empty
will extract that entire row or column
for example we can go into the object "mat"
or matrix, extract row 2,
all columns. this will extract
all the elements in row 2
now let's go ahead and extract from this matrix
all rows and just column 1
as we saw with vectors we can perform
element-wise addition, subtraction, multiplication
or division; here we can take our matrix
and multiply every element by 10
in the next video we will discuss importing data into R
and working with a set of data
Thanks for watching this video and make sure to check out my other instructional videos