Vous êtes sur la page 1sur 2

Hello, and welcome.

In this video, we will show you how to create vectors and


factors in the R programming language.
A vector is a one-dimensional array of objects,
and its a simple tool to store your data. In R, there is no restriction
on the type or number of elements that a vector can contain. Lets create a vector
that contains the run times of
that contains the run times of Toy Story and Akira.
To do this, we simply use the c command, and inside the parentheses, well
write the two values that we want
to include in our vector. For purposes of illustration, well also divide the
vector by 60.
This division operation will be applied to each element individually.
We can also assign a vector to a variable, which will give us the same result.
Its easy to create a vector that contains a sequence of elements. So lets say
you wanted a vector that contains the numbers from one to ten, in order.
You could type all the values out, or you could simply write one, then a colon,
then ten.
The result will be the same. If you want the numbers in decreasing order,
simply switch the 1 and the 10. And you can see how that would look. As a quick
note,
take a look at the pound sign to the right of the c command. In R,
this symbol is used to write comments in your code.
Everything to the right of the symbol is for the programmer only, and will not be
interpreted.
So we just saw that we can create vectors that hold numerical data, like a set of
years.
But we can also create a character vector, if we want to store a list of strings.
For example, we can create a vector that holds titles, which would look like this.
One additional vector type well touch on is the logical vector.
Logical data is simply a True or False, typically created via a comparison
operator.
For example, if we type 1997 greater than 2000, we get False as the output.
These operators can be applied to vectors.
So suppose we have a vector that contains movie ratings,
which looks like this. If we run movie ratings greater than 7.5,
R will check each element in the vector to see if the value is greater than 7.5.
If so, the resulting value will be true, and if not, the value will be false.
You can see how that would look in the output here.
In R, factors are variables that can take on a limited number of values.
These variables are often referred to as categorical variables. The other extreme
would be a continuous variable,
which can take on an infinite number of value
Lets encode the vector we have here as a factor.
If you look at the output, youll notice that there are 3 levels, which correspond
to the
3 unique categories of the vector: Animation, comedy, and crime.
The summary function is useful when working with factors. If we apply
summary on the original vector, youll notice that it simply provides some basic
information
about the vectors structure and content. But if we apply it on the factor,
the output shows us the number of occurrences of each of the component categories.
There are two types of categorical variables
A nominal categorical variable does not have any implied order.
The previous examples with movie genres were nominal, since there is no clear
ordering between categories
like Animation and comedy. Ordinal variables, on the other hand, do have an
ordering.
So consider the vector here. It should be apparent that certain categories are
less than others,
and wed want to encode this information into a factor.
To do this, we need to pass two additional arguments to the factor function.
We need to specify that ordered = TRUE, and then we need to specify a vector with
the levels.
Notice that the categories in the vector are listed in the order that we want,
from lowest to highest so to speak.
If you take a look at the output, you can see that this information has been
successfully encoded.
By now, you should understand how to create various types of vectors,
as well as how to use the factor function.
Thank you for watching this video.

Vous aimerez peut-être aussi