What is the c() function in r?

The function c in r is the most often used function in the R programming language. It is used in creating vectors and data frames. As a result, unless you are getting all of your data from outside sources you really cannot do anything in R without knowing how to use this function. It makes it an invaluable function for you to learn and understand.

Description – c function in R

The c function is an extremely easy function to use, and it has the format of c(x1, x2…) where “x1”, “x2” and so on represents the data being combined by this function. This function turns the data series into an r vector. It can also be used to create the columns of a data frame, however all it is really doing, in this case, is creating vectors within the data frame. You can apply it to a group of data frames, but you get some unusual results. As a result, it is most effective when creating vectors, or data frame columns. It is an easy function to use because its arguments are simply a series of data points.

Explanation

The c function is most often used to combine data into a vector. It simply takes the content given to the function as arguments and combines it into a single data set. While its primary use is the creation of vectors it can be used with other R objects as well, but the results are not always useful. For example, when you use it on a data frame, it just produces a list of the columns even if the columns had the same name. You would expect them to combine any columns that have the same name, but it does not do so, and the results are not a data frame.

Examples – c function in R

Here are five examples, demonstrating the C function in action. each of them shows the function being used under different circumstances.

> x = c(1, 2, 3, 4, 4 , 6, 7 , 8, 9 , 10)
> x
[1] 1 2 3 4 4 6 7 8 9 10

This example simply produces a numeric vector, from a list of numbers placed in the function.

> x = c(“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”)
> x
[1] “A” “B” “C” “D” “E” “F” “G” “H” “I” “J”

This example simply produces a character vector, from a list of letters placed in the function.

> x = c(TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE)
> x
[1] TRUE FALSE FALSE TRUE TRUE FALSE TRUE

This example simply produces a logical vector, from a list of true and false values placed in the function.

> a = c(1, 2, 3, 4, 5)
> b = c(5, 4, 3, 2, 1)
> x = c(a, b)
> x
[1] 1 2 3 4 5 5 4 3 2 1

This example combines two numeric vectors to produce a new numeric vector consisting of the values of the other two vectors.

> a = c(1, 2, 3, 4, 5)
> b = c(“A”, “B”, “C”, “D”, “E”)
> x = c(a, b)
> x
[1] “1” “2” “3” “4” “5” “A” “B” “C” “D” “E”

This example combines character vectors and numeric vectors to produce a new character vector consisting of the values of the other two vectors. This demonstrates combining vectors of different types.

Applications – c function in R

The c function has endless applications because it is primarily used to produce vectors and data frame columns. Anytime you combine information into a data frame or vector, you are using this function to combine it into a single object. These include the creation of mathematical vectors in the format of R vectors. If you are creating a data frame for the purpose of creating a graph, this is a crucial step in entering the values you are trying to graph.

You will use the c function more than any other function in the R programming language. As a result, you need to understand how to use it properly. Fortunately, it is an easy function to master, because while it has multiple arguments, it is just a simple list of values being combined into one object.

Scroll to top
Privacy Policy