R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
In R, the c()
function is primarily used to concatenate or append values to vectors. The name "c" stands for "combine." In this tutorial, I'll guide you through the operation of appending values to vectors in R:
You can create a basic vector using the c()
function:
vec1 <- c(1, 2, 3, 4, 5) print(vec1) # [1] 1 2 3 4 5
To append a single value to a vector:
vec1 <- c(vec1, 6) print(vec1) # [1] 1 2 3 4 5 6
To append multiple values to a vector:
vec1 <- c(vec1, 7, 8, 9) print(vec1) # [1] 1 2 3 4 5 6 7 8 9
You can also append an entire vector to another vector:
vec2 <- c(10, 11, 12) vec1 <- c(vec1, vec2) print(vec1) # [1] 1 2 3 4 5 6 7 8 9 10 11 12
append()
FunctionR provides an append()
function, which is especially handy when you want to insert values at a specific position in the vector:
vec3 <- append(vec1, c(13, 14), after=5) print(vec3) # [1] 1 2 3 4 5 13 14 6 7 8 9 10 11 12
In the example above, the values 13 and 14 were inserted after the fifth element of vec1
.
While appending typically refers to adding to the end, you might sometimes want to add values to the beginning of a vector. This is essentially the same operation, just with values specified in a different order:
vec1 <- c(0, vec1) print(vec1) # [1] 0 1 2 3 4 5 6 7 8 9 10 11 12
That wraps up the basics of appending values to vectors in R. Practice these operations to become more comfortable with vector manipulations in R!
R Append Vectors Example:
# Create two vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) # Append vectors using c() appended_vector <- c(vector1, vector2)
How to Concatenate Vectors in R:
# Create two vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) # Concatenate vectors concatenated_vector <- c(vector1, vector2)
Using c() Function for Vector Append in R:
# Create two vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) # Append vectors using c() appended_vector <- c(vector1, vector2)
Appending Elements to a Vector in R:
# Create a vector original_vector <- c(1, 2, 3) # Append elements to the vector appended_vector <- c(original_vector, 4, 5, 6)
Append Vectors Horizontally in R:
# Create two vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) # Append vectors horizontally appended_matrix <- cbind(vector1, vector2)
Appending Vectors with Different Lengths in R:
# Create two vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6, 7) # Append vectors with different lengths appended_vector <- c(vector1, vector2)
Append Vector to Data Frame in R:
# Create a data frame data_frame <- data.frame(A = c(1, 2, 3), B = c(4, 5, 6)) # Append a vector to the data frame new_vector <- c(7, 8, 9) data_frame$C <- new_vector
Combine Multiple Vectors into One in R:
# Create three vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) vector3 <- c(7, 8, 9) # Combine vectors into one combined_vector <- c(vector1, vector2, vector3)
Appending NA Values to a Vector in R:
# Create a vector original_vector <- c(1, 2, 3) # Append NA values to the vector appended_vector <- c(original_vector, NA, NA, NA)
Appending Character Vectors in R:
# Create two character vectors char_vector1 <- c("apple", "banana", "orange") char_vector2 <- c("grape", "kiwi", "mango") # Append character vectors appended_char_vector <- c(char_vector1, char_vector2)