c() Function in R (2 Examples)
This article illustrates how to combine values into a vector or list using the c() function in R.
The content looks as follows:
Here’s the step-by-step process…
Definition & Basic R Syntax of c Function
Definition: The c R function combines multiple values into a vector or list.
Basic R Syntax: You can find the basic R programming syntax of the c function below.
c(value1, value2) # Basic R syntax of c function
In the following, I’ll show you two examples for the application of the c function in the R programming language.
Example 1: Combine Numeric Values in Vector Using c() Function
In Example 1, I’ll explain how to use the c function to create a new vector object that contains several numeric values.
For this, we simply have to specify all values we want to add to our new vector within the c function. We have to separate these values by a comma:
x1 <- c(1, 5, 3, 8, 8, 6) # Applying c() function
Let’s have a look at the data object x1 that we have just created:
x1 # Print output # 1 5 3 8 8 6
As you can see in the RStudio console, our new vector consists of the numeric values we have specified within the c function.
Example 2: Combine Variable & Numeric Values
Example 2 explains how to concatenate an already existing vector object with new values. For this example, we’ll use the vector object created in Example 1. We can append new values to this vector as shown in the following R code:
x2 <- c(x1, 5, 7, 1) # Concatenate variable & values
The final output consists of the input vector x1 and the new elements that we have specified within the c command:
x2 # Print output # 1 5 3 8 8 6 5 7 1
Video, Further Resources & Summary
Do you need further info on the examples of the present article? Then I can recommend to watch the following video of my YouTube channel. In the video, I show the topics of this article:
The YouTube video will be added soon.
Furthermore, I can recommend to have a look at the related tutorials on this website.
- Append Value to Vector
- combine R Function of dplyr Package
- R Functions List (+ Examples)
- The R Programming Language
In this tutorial, I illustrated how to apply the c function in the R programming language. Please let me know in the comments below, if you have further comments or questions. Furthermore, don’t forget to subscribe to my email newsletter for updates on new tutorials.
Statistics Globe Newsletter