In R, you can create a vector using the c() function, which stands for "combine" or "concatenate". The c() function allows you to combine multiple values into a single vector. Here are a few ways to create a vector:
- Creating a Numeric Vector:
numeric_vector <- c(1, 2, 3, 4, 5) # Creates a numeric vector with values 1, 2, 3, 4, 5
- Creating a Character Vector:
character_vector <- c("apple", "banana", "orange") # Creates a character vector with strings "apple", "banana", "orange"
- Creating a Logical Vector:
logical_vector <- c(TRUE, FALSE, TRUE) # Creates a logical vector with values TRUE, FALSE, TRUE
- Creating a Vector with Mixed Data Types:
mixed_vector <- c(1, "hello", TRUE) # Creates a vector with numeric, character, and logical values
- Creating a Sequence of Numbers: You can use the : operator to create a sequence of numbers. For example, to create a vector containing numbers from 1 to 5:
sequence_vector <- 1:5 # Creates a numeric vector with numbers 1, 2, 3, 4, 5
Once you have created a vector, you can perform various operations on it, such as indexing, subsetting, applying functions, and more. Vectors are fundamental objects in R and are widely used for data storage, manipulation, and analysis.