Within the r programming language, vectors are one of the primary data types. Not only are they a main data structure but they help form the base for other data structures such as data frames through their use in R internal code. As a result, it is important to understand the R function that is used to create them – c () in R.
The c function is ubiquitous in r code samples. It’s a quick way to turn a list of argument values into an R vector, one of the essential building blocks (along with the data frame) of core R code. But the c function is more than just a R vector constructor.
The c function has an interesting side effect when constructing an R vector. It removes all data element attributes except names. This is useful when you need to clean up and standardize data. The function itself is short for “combine”, the combination being the resulting vector. It’s faster and more efficient than append() due to the underlying c code.
Many tutorials will group vectors by the different types of data type(s) they contain. A numeric vector is a vector of numbers. Within the world of numeric vector(s), you can get more specific such as an integer vector (all integer numerical value items), A character vector is a vector of character strings. The operation of the c() function treats these equally however – it takes a row of values and returns a new vector data object.
Description – C function in R
A vector is an important r object and creating them involves using c in r. This c function has the format of c(data) where “data” is the element list for the vector. Vectors are a group of related values. While they are different from their mathematical counterparts, they can be used to store the coordinates for mathematical vectors in any number of dimensions. When programming in r, vectors can hold a variety of data types. This makes them useful for all sorts of applications. Furthermore, vectors can be used to form larger data structures such as data frames. Because the c function is used to create vectors, you will use it a lot in the r programming language.
Explanation of The C function in R
The c function is used to create vectors. This includes an atomic vector which can have value types of integer, double precision numeric, complex, character, and raw. A character vector is similar to a character string, but they are a completely different type of variable. The c function creates a group from the individual values being included. These values must be the same type, but they can be any type. for example, you can have a vector of character strings. This makes the c function a useful and highly flexible function. When programming in r you will be using this function a lot.
Examples of the C function in R
Here are four examples of r code showing the c function in action.
> x = c(1:5)
> x
[1] 1 2 3 4 5
In this example, we use the function to set up a simple string of numbers.
> y = c(1,3,5,7,9)
> y
[1] 1 3 5 7 9
In this example, we use the function to set up a vector of odd numbers.
> z = c(“A”, “B”, “C”, “D”, “E”)
> z
[1] “A” “B” “C” “D” “E”
In this example, we use the function to set up a vector of individual characters.
> xyz = c(“Tiger”, “Corny”, “Secil”, “nutmage”, “Grout”, “Quantum”, “Atari”)
> xyz
[1] “Tiger” “Corny” “Secil” “Nutmage” “Grout” “Quantum” “Atari”
In this example, we use the function to set up a vector of character strings. Specifically the names of cats.
Special case – using the colon operator for sequences
There’s a shortcut you can use if you need to enter a sequence of numeric value data object(s) into the c function – look at the colon operator. You give the colon operator two parameters – a (starting point) and b (ending point). It generates a sequence from a to b in steps of 1 or -1 where value of b is included in the sequence. This saves you writing a loop.
Potential Applications of the c function in R
The c function has as many applications as vectors do. In creating a vector from within a script file, you simply equate the vector name to this function with the proper arguments. You can use them to store a group of numbers, letters, words, and any other values you can store in a variable.
Another application of this function is in creating data frames where the resulting vectors are the columns. Because vectors have so many uses within r the applications of this function are literally endless.
The c function is used to combine groups of values into vectors. These vectors can also be used as columns in data frames. It is a function that you will find yourself using repeatedly. As a result, it is one that you want to get to know well. It is worth taking the time to learn every aspect of this function.