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 examples:
- Numeric Vector:
numbers <- c(1, 2, 3, 4, 5) # Creates a numeric vector with values 1, 2, 3, 4, 5
- Character Vector:
names <- c("John", "Alice", "Bob") # Creates a character vector with names "John", "Alice", "Bob"
- Logical Vector:
logical_values <- c(TRUE, FALSE, TRUE, TRUE) # Creates a logical vector with values TRUE, FALSE, TRUE, TRUE
- Vector with Mixed Data Types:
mixed_vector <- c(1, "hello", TRUE) # Creates a vector with numeric, character, and logical values
You can also create a sequence of numbers using the : operator. For example:
sequence <- 1:10 # Creates a numeric vector with numbers from 1 to 10
Once you have created a vector, you can perform various operations on it, such as indexing, subsetting, applying functions, and more.