Like all programming languages, R allows you to create variables. Here’s an example of how to define a variable in R:
diameter_of_pizza <- 16
This line, which defines a variable called diameter_of_pizza
, might be useful if you’re writing code for a pizza restaurant to store information about orders. Keep in mind that the variable diameter_of_pizza
can only be equal to one number at a time, because it’s something called a scalar (a single-number variable). Right now, it’s equal to 16. Later, if your restaurant gets an order for a 12-inch pizza, you can change its value easily:
diameter_of_pizza <- 12
When your restaurant receives more than one order at a time, you will want to keep track of all of the orders at once. That’s why R makes it possible to create vectors, which are variables that can contain arrays of different values. For example, suppose that your restaurant receives orders for 3 different pizzas at the same time. You could store all of the pizza diameters in one vector variable very easily:
diameter_of_pizza <- c(8,16,20)
Here, we used the c()
function to create a vector. The “c” is short for “combine”, because it enables us to combine multiple values together and store them in one vector variable. You can print out this new vector variable as follows:
print(diameter_of_pizza)
When you run this line of code, you’ll see the following output:
[1] 8 16 20
You can see that R has printed 3 numbers rather than just one, since diameter_of_pizza
is a vector variable – one that contains an array of unique values. Congratulations – you have created a vector in R!
After creating a vector, we can alter its contents. For example, if we want to change the third pizza order from a 20-inch diameter to a 21-inch diameter, we can redefine the value of the third element of the vector as follows:
print(diameter_of_pizza)
We can see that only the third element has changed:
[1] 8 16 21
Vectors are useful because we can do operations on entire vectors at once, saving time and space. For example, if we want to add six inches to every pizza order in our vector, we can do it as follows:
diameter_of_pizza <- diameter_of_pizza + 6
When you print out the vector using print(diameter_of_pizza)
, you’ll see that one line of code has added 6 inches to every element of the vector:
[1] 14 20 27
There are other ways to create vectors. One simple shortcut you can use is the colon “:”, which you can use as follows:
new_vector <- 1:10
This line of code creates a vector of all of the integers between 1 and 10. You can run print(new_vector)
to see the output:
[1] 1 2 3 4 5 6 7 8 9 10
You can also create vectors using the vector()
command. You can use this method if you intend to define each element of the vector, one at a time:
y <- vector(length=2)
y[1] <- 3
y[2] <- 7
Here, we defined a vector called y with length 2, and then we defined both of its elements as 3 and 7, respectively. When you run print(y)
, you can see that we’ve successfully defined both elements of the vector:
[1] 3 7
Vectors can contain other data types besides numbers. For example, we can also define a vector of strings:
string_vector<-c('Call','me','Ishmael')
You can even create a vector of vectors:
vector_of_vectors<-c(c(1,2,3),c(4,5,6),c(7,8,9))
However, when you create a vector of vectors, R automatically does what’s called “flattening”, or in other words it combines every element of every vector into one single vector. You can see the vector by running print(vector_of_vectors)
, which will give you the following output:
[1] 1 2 3 4 5 6 7 8 9
Interested in learning more about R? Take Udacity’s Programming for Data Science with R course and learn the fundamentals of R including data structures, variables, loops, functions and more.