[MUSIC PLAYING]
SPEAKER: Well, hello, one and all, and welcome to our short on vectors.
We'll take a look at how we can use these things called vectors
to store information and also how to quickly build up
vectors for use in R. Now, vectors are this way of storing data that
has all the same type, or we might say in R, the same storage mode,
for instance, all numbers or all character strings.
And they're really handy to build up all kinds of other structures.
So we'll focus on them today and see if we can actually
quickly make a lot of them using functions built into R.
So let's say I want to have a vector here of words.
And to make a new vector, I can actually use this function called c(),
where c() stands for Combine.
This is a pretty key idea with vectors.
I'm combining different individual pieces of data into one vector.
So here, I might have a vector of words, like "It's", "a", "beautiful", "day",
just like this.
And I can store it, let's say, inside of this object that I'll call words.
And I can run this on line 1.
And we'll see that it has been run down in my R console down below.
So if I want to see what's inside of this words vector,
I can simply type "words" and run that line.
And I'll see that I have down below those four words--
"It's" "a" "beautiful" "day."
And again, this works because this vector
is created from individual pieces of data that are all of the same type,
in this case, all character strings.
So c()-- great function to get to know to build up vectors from scratch.
But if you want to make other kinds of vectors, if you want to manipulate them,
it's worth thinking about other functions that
can help you do so pretty quickly.
Let's say I wanted to have this vector, "It's", "a", "beautiful", "day".
But I want to repeat these words, these four words, multiple times,
like "It's" "a" "beautiful" "day", "It's" "a" "beautiful" "day",
"It's" "a" "beautiful" "day."
I can actually do that using a function, one called rep() where rep() stands
for Repeat.
And rep() can actually take as input a vector and very quickly make for me some
new vector based on that old vector.
Let's try this out.
The first argument to rep() is the vector to begin with, so in this case,
words.
Or it could even be a single value.
And here, one of the arguments to rep() is one called times.
So I can say, times = 2 here.
And this will actually take my vector, "It's", "a", "beautiful", "day",
and give me a new vector with this, "It's", "a", "beautiful", "day",
repeated two times.
I'll try running this here.
And we'll see down below that I get "It's" "a" "beautiful" "day",
"It's" "a" "beautiful" "day".
Pretty good.
I could even do times = 3.
And now I'll get that repeated three times down below as well.
So rep() good for repeating things some number of times.
But it turns out rep() also has another argument, one called each.
So notice how when I used times, this took my vector as it is--
"It's", "a", "beautiful", "day"-- and repeated these values in that same order
two or three different times.
Well, if I use each, that will actually look at every individual element I have
in my vector, like "It's", "a", "beautiful", "day," individually now,
and repeat each of those two times.
So let's see what happens here.
I'll run this.
And I'll get "It's" "It's" "a" "a" "beautiful" "beautiful" "day" "day".
So less of a utility here, but you could imagine
this being useful if you want to transform a vector where every element
you have might be repeated each some number of times.
So rep() a good tool to have in your toolkit when working with vectors like
these.
Let's think of other kinds of vectors, though.
So this is here a character vector, one that is filled with character strings.
But, of course, vectors can also be composed of numbers.
So let me clear my console.
And let's try making a new vector, one called numbers.
And I'll, just for simplicity, make this a vector of 1, 2, 3, 4, 5, 6, 7, 8, 9,
and 10.
So this is my vector of numbers 1 through 10.
If I run line 1 to assign this vector to the object numbers and type "numbers"
down below, we'll all see that vector down below as well.
So c() again coming in handy when I want to make, in this case,
a vector of some numbers.
But you could imagine it's getting pretty tiring pretty quickly
if I were to enter in beyond just 10, like 100 or 200 or so.
And I wanted 1, 2, 3, all the way up to some number.
So thankfully, R comes with the ability to use what's called this colon here,
which will give me a vector of numbers, starting with the first one, inclusive,
and ending with the last one, inclusive, and going up in ascending order one
number at a time.
So here, if I run line 1 and line 3, I'll
get the same result but with much fewer lines--
well, much less typing, let's say.
I could even do this.
I believe I could do without the c().
And I could do numbers 1 through 10, and same thing here.
So 1:10 actually creates the vector for me.
I don't need c() to do that in this particular case.
Now, what else could we do when it comes to things like these sequences?
You might often want to create some sequence of numbers.
But you might not always want it to be in ascending order one number at a time.
So there is a function to be aware of, one called seq(),
which stands for sequence, and allows you to construct numeric vectors
by specifying some start point, some end point, and some, let's say--
what do you call it-- maybe an increase along the way.
So here, why don't we try this from argument to seq() and start with 1,
and the "to" argument to seq() and start with 10.
And this will by default give me the same thing we said before,
a sequence of numbers 1 through 10.
I'll hit Enter on this, Enter on this.
And now we'll see here that I get 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
so sequence then doing the same work we had with 1:10.
But let's say I want to get a little more fancy with this.
I certainly can.
Seq() actually has as an argument as well an argument called "by."
And by specifies a number to increase by as we go through
and create our sequence.
So to be clear, we would start at 1 and then jump up two to 3
and then jump up two to 5, all the way until we get to 10 exactly or above 10.
So let's see what happens here.
I'll run numbers.
I'll run numbers and then numbers again.
And here, we see 1, 3, 5, 7, 9, so now increasing, we'll see, by 2 each time.
Well, one other thing we can use sequence for
is maybe you want a vector of some specific length.
And you want it to start with some number and end with some number.
And all numbers in the middle should be equally spaced apart.
Well, seq() can do that as well using an argument called length.out.
A bit of a weird argument name here, but length.out out
can take as input some number and create a sequence for us
where the first number is 1, the last number is 10,
and the total vector's length will be this number here, in this case, 3,
with all the numbers evenly spaced apart.
So let's try this out.
I'll run line 1 here, and I'll run line 3.
And now we'll see, true to its word, I get
a vector of three numbers, where each is evenly spaced out.
And it begins with 1 and ends with 10.
So this was a brief foray into vectors how to create them manually with c(),
how to repeat them using rep(), how to, let's say,
get a sequence of values using colon, and how to modify or adjust that
sequence using, in this case, the seq() function.
So this then was our short on vectors.
And we'll see you next time.