Vectors were discussed in a previous post. In short, vectors are a collection of information. Functions serve the purpose of performing several operations one after another. Now, will combine these two concepts into what are known as vectorized functions. Vectorized functions are functions that perform several operations on a vector. In other words, you can put several numbers into a vector and then have a function do something to all the numbers at the same time.
Here is an example
You have been asked to keep score during basketball season. Below are the number of points per game for a player name James for his first six games. The information is inputted in R as follows
-
> points.of.James <- c(24, 8, 8, 12, 18, 9) > points.of.James [1] 24 8 8 12 18 9
What we have done so far is create the variable “points.of.James” and assigned the vector of 24, 8, 8, 12, 18, 9. The points represent the number of points he scored in the six games.
The first function we are going to use is the sum() function. This function will add up how many points James scored in the six games. Below is the code for it.
-
> sum(points.of.James) [1] 79
Here is what we did
- We told R we want the sum of the values in the variable “points.of.James”
- The values in the variable “points.of.James” are the vector 24, 8, 8, 12, 18, 9.
- R adds the values up and produces the answer 79
Another more complicated example has to do with how functions can combine vectors. For example, let’s say a list of people in the English department and you want to add after their name that they all have the position of lecturer. To do this you need to use the paste() function which will combine the information. Below is an example of how to do this using R.
-
> faculty.names <- c("Darrin Thomas", "John Williams", "Sarah Smith") > Rank <- "Lecturer" > Rank <- ", Lecturer" > paste(faculty.name, Rank, sep = "") [1] "Darrin Thomas, Lecturer" "John Williams, Lecturer" "Sarah Smith, Lecturer"
Here is what we did.
- We created a variable called “faculty.names” and we assigned a vector to it that contain the following names
- Darrin Thomas
- John Williams
- Sarah Smith
- When then created a variable called “Rank” and assigned the value “, Lecturer”. We put the comma in front of the word Lecturer so that when we combine the faculty.names and rank variables we have a comma after the name of the faculty person and before their rank, which is lecturer.
- Next, we combined all of the names of the faculty members with the rank of “Lecturer” using the paste() function.
- NOTE: In the paste function we included the argument sep = “” This prevents a space appearing after the name of the person when the comma is inserted. Below is an example of what we do not want
- Darrin Thomas , Faculty (Remeber we do not want this space. That’s why we use the sep = “” argument because it removes the space in front of the comma)
- NOTE: In the paste function we included the argument sep = “” This prevents a space appearing after the name of the person when the comma is inserted. Below is an example of what we do not want
- After setting up the paste function we get the “faculty.names” and “Rank” combined which shows the names of the faculty members with their corresponding rank.
Let’s pretend you are dealing with the same problem but now the faculty members have different rankings. Here is the code for how to do this.
-
> faculty.names <- c("Darrin Thomas", "John Williams", "Sarah Smith") > faculty.ranks <- c(", Lecturer", ", Senior Lecturer", ", Principal Lecturer") > paste(faculty.names, faculty.ranks, sep="") [1] "Darrin Thomas, Lecturer" "John Williams, Senior Lecturer" [3]"Sarah Smith, Principal Lecturer"
All that is new is that we created a variable called “faculty.ranks”, which included the new rankings. Then we used the paste function to combine the names with ranks. As you can see, each person can their corresponding rank. The first name got the first rank, the second name got the second rank, etc. If there is a difference between the number of names and ranks R will recycle values to complete the function
Pingback: Introduction to Vectors in R: Part II | educationalresearchtechniques
Pingback: Basics of Vectors Functions in R | Education an...