## Working with Vectors

**Course Description**

A vector is a set of values with the same data type or what we see as a column in a data set. This basic R object can be created in R using the C function which combines the values to form a vector.

**What You'll Learn**

** > **How to create a vector of values

**> **Access a specific element or elements within a vector

**> **Apply conditions to vector values

**> **Get the length of a vector, and more ways to work with vectors.

If you haven’t installed R and Rstudio already, you can watch "**Getting started with Python and R for Data Science**" video to get started.

For the dataset used in this exercise, download from **here**.

A vector is a set of values with the same data type or what we see as a column in a data set. This basic R object can be created in R using the C function which combines the values to form a vector.

So, let’s create a simple vector column called "animals" and feed the C function a set of values of the same data type to form vectors. We’ll call it, say, “animals" example here, and I’m just gonna give it a set of values in the C function and “bird”. That will do. And let’s create another vector on the weight of each animal in pounds and just deliberately going to make this a missing value here.

Now, to access a specific element or elements within a vector, we can simply use square brackets, the same we did when we extracted rows and columns in our data frame video. For example… So, here, this gets us the first element or data value in the "animals" vector. We can also get a range of values in the vector. For example, I’m interested in the first two and we can apply conditions to vector columns as we did in the operators video.

We can also use the length function on a vector so we don’t need to manually find or know how many values are in a vector. We can replace missing values in a vector too and this can be simply done by... so, open those square brackets again and we want to find anything that is a missing value in the weight variable, of course. We’re gonna replace them, say, with the value “12” and we can do calculations on numeric vectors. Let’s say we want to take the mean of “weight”. I want to average across all those weights. We can sort the elements in a vector. For example, you might want to sort from the lowest to the highest, if it’s a numeric value, or alphabetical order if it’s more categorical values. This sorts it in alphabetical order

You can create a new vector using a range of numbers in a sequence. So, let’s create a new variable called “class.tag” here and we want it to have a range from 1 to 3 and, finally, you can organize all your vectors into a data frame. So, let’s just call our data frame “animal.data” and we’ll use the “data.frame” function here and within this, we are going to add all our vectors. So, animals, weight, and “class.tag”, and let’s have a look at this. Okay, cool.

That’s the basics of vectors in R. Another important object in R is factor. So let’s move on to this in the next video.

**Rebecca Merrett** - Rebecca holds a bachelor’s degree of information and
media from the University of Technology Sydney and a post graduate diploma in
mathematics and statistics from the University of Southern Queensland. She has
a background in technical writing for games dev and has written for tech
publications.