Creating a vector is something that all programming enthusiasts need to learn, no matter the language. R is no exception, and there are quite a lot of methods that allow you to do so. One of them is c().
To make it easier for you, we have prepared this article to explain just what c() in R is.
What Is c() In R?
c() is a generic built-in function in the R language. What we call c() is the shortened name for combine().
As the name suggests, its main job is to combine the arguments together. This function produces a vector carrying explicitly providing values. In other words, it combines the inputs together and makes a new vector.
As you can easily guess, this nature requires that all input arguments must have a common type. This type should also be the same as the output value. There should be no attribute left, aside from the names.
It has the following syntax:
c(…, recursive = FALSE, use.names = TRUE)
You can easily observe that the syntax has a total of three parameters, recursive, use.names, and ….
Of these, you should pay the most attention to the … variable, as it stands for the objects on which the concatenation will proceed.
The next variable is recursive, which operates as a logical function. When it has a TRUE value, the process will be performed recursively. In other words, it will descend through all the lists as well as pairlists to combine all available elements and make a vector.
As for use.names, it is also a logical function. Its main job is to tell the program if you want to preserve the names. The function will save the names with a TRUE value, and all other attributes get deleted. On the other hand, it will delete everything.
This function either returns a NULL value or a vector’s expression with the appropriate mode.
Example: Using c() in R To Combine Vector’s Numeric Values
One of the most used applications for c() in R is making a brand new vector object with numerous numeric values.
All you need to do for it to work is specify the values that you want to combine into the new vector. Then, you just put them all into the function. Do remember to separate them with commas, though.
x <- c(2, 5, 4, 8, 5, 6) x
The output should be as follows:
# 2 5 4 8 5 6
As you can see, all the numbers have been added to the vector with no apparent issue.
Example: Using c() in R To Combine Numeric Values With Variable
Combining numbers is just the start of this function. Once you have familiarized yourself enough, you can start using it to combine values with existing variables.
In other words, you can make changes to your vectors, as they still count as a variable.
Let’s take the vector in the example above and add in some additional value.
x1 <- c(x, 3, 9, 1) x1
The output is now:
# 2 5 4 8 5 6 3 9 1
This article has introduced you to all the necessary knowledge regarding c() in R. While the examples we provided are all on the basic level, this function can be used in some creative ways. We believe it can be a useful tool in your arsenal throughout your journey in learning R.