51 lines
2.2 KiB
R
51 lines
2.2 KiB
R
|
# 1. Using the c() function to create a numeric vector
|
||
|
numeric_vector <- c(1, 2, 3, 4, 5)
|
||
|
print("Numeric Vector:") # Output: [1] "Numeric Vector:"
|
||
|
print(numeric_vector) # Output: [1] 1 2 3 4 5
|
||
|
|
||
|
# 2. Using the c() function to create a character vector
|
||
|
character_vector <- c("apple", "banana", "cherry")
|
||
|
print("Character Vector:") # Output: [1] "Character Vector:"
|
||
|
print(character_vector) # Output: [1] "apple" "banana" "cherry"
|
||
|
|
||
|
# 3. Using the c() function to create a logical vector
|
||
|
logical_vector <- c(TRUE, FALSE, TRUE, FALSE)
|
||
|
print("Logical Vector:") # Output: [1] "Logical Vector:"
|
||
|
print(logical_vector) # Output: [1] TRUE FALSE TRUE FALSE
|
||
|
|
||
|
# 4. Using the seq() function to create a sequence of numbers
|
||
|
sequence_vector <- seq(1, 10, by=2)
|
||
|
print("Sequence Vector (by 2):") # Output: [1] "Sequence Vector (by 2):"
|
||
|
print(sequence_vector) # Output: [1] 1 3 5 7 9
|
||
|
|
||
|
# 5. Using the rep() function to create a repeated vector
|
||
|
repeated_vector <- rep(x = 6, times = 4)
|
||
|
print("Repeated Vector:") # Output: [1] "Repeated Vector:"
|
||
|
print(repeated_vector) # Output: [1] 6 6 6 6
|
||
|
|
||
|
# 6. Using the : operator to create an integer sequence
|
||
|
colon_vector <- 1:10
|
||
|
print("Colon Operator Vector:") # Output: [1] "Colon Operator Vector:"
|
||
|
print(colon_vector) # Output: [1] 1 2 3 4 5 6 7 8 9 10
|
||
|
|
||
|
# 7. Using the seq_along() function on another vector
|
||
|
along_vector <- seq_along(c("x", "y", "z"))
|
||
|
print("Seq Along Vector:") # Output: [1] "Seq Along Vector:"
|
||
|
print(along_vector) # Output: [1] 1 2 3
|
||
|
|
||
|
# 8. Using the seq_len() function to generate a sequence of given length
|
||
|
length_vector <- seq_len(5)
|
||
|
print("Seq Length Vector:") # Output: [1] "Seq Length Vector:"
|
||
|
print(length_vector) # Output: [1] 1 2 3 4 5
|
||
|
|
||
|
# 9. Combining different types in a vector (coerced to the same type)
|
||
|
mixed_vector <- c(1, "two", TRUE)
|
||
|
print("Mixed Type Vector (coerced):") # Output: [1] "Mixed Type Vector (coerced):"
|
||
|
print(mixed_vector) # Output: [1] "1" "two" "TRUE"
|
||
|
|
||
|
# 10. Using the gl() function to generate factors
|
||
|
generated_vector <- gl(n = 3, k = 2, labels = c("Group1", "Group2", "Group3"))
|
||
|
print("Generated Factor Vector:") # Output: [1] "Generated Factor Vector:"
|
||
|
print(generated_vector) # Output: [1] Group1 Group1 Group2 Group2 Group3 Group3
|
||
|
# Levels: Group1 Group2 Group3
|