# Working with Vectors in R | Beginning R Programming – Part 7

June 25, 2019 9:33 pm

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.

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 a 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

like the same we did when we extracted

rows and columns in our data frame video

so for example…

so this 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 so, for example,

I’m interested in…

the first two

and we can apply conditions to

vector columns like we did in 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

and we’re gonna replace them

say with the value “12”

and we can do calculations

on numeric vectors

so 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

so, for example, you might

want to sort from lowest to 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

and 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

**Prerequisites****:**

Download the Data Set

How to Install R

**Part 8: **

Understanding Factors in R

**Part 6: **

Data Frames

**Full Series:**

Beginning R Programming

**More Data Science Material:**

[Video] How to do the Titanic Kaggle competition in R

[Video] Introduction to dplyr

[Blog] Keeping Your Ethics in Check as a Data Scientist

[Blog] University of New Mexico Data Science Certificate Program

(187)