In R, a vector is a sequence of data elements of the same basic type.
The c() function is used to create a vector.
# A numeric vector
numeric_vector <- c(1, 2, 3, 4, 5)
print(numeric_vector)
## [1] 1 2 3 4 5
# A character vector
character_vector <- c("a", "b", "c", "d", "e")
print(character_vector)
## [1] "a" "b" "c" "d" "e"
my_numeric_vector with
the numbers 10, 20, 30, 40, and 50.# Your code here
my_numeric_vector <- c(10, 20, 30, 40, 50)
print(my_numeric_vector)
## [1] 10 20 30 40 50
my_character_vector
with the names of your three favorite colors.# Your code here
my_character_vector <- c("blue", "green", "yellow")
print(my_character_vector)
## [1] "blue" "green" "yellow"
my_numeric_vector.# Your code here
length(my_numeric_vector)
## [1] 5
# Your code here
mixed_character_vector <- c(10, "Leo", TRUE)
typeof(mixed_character_vector)
## [1] "character"
print(mixed_character_vector)
## [1] "10" "Leo" "TRUE"
c(1, 2, 3) and
c(4, 5, 6).# Your code here
added_vectors <- c(1, 2, 3) + c(4, 5, 6)
print((added_vectors))
## [1] 5 7 9
c(1, 2, 3) and c(4, 5).# Your code here
mismatched_vectors_added <- c(1, 2, 3) + c(4, 5)
## Warning in c(1, 2, 3) + c(4, 5): longer object length is not a multiple of
## shorter object length
print(mismatched_vectors_added)
## [1] 5 7 7
my_results. Then, append
the number 10 to it.# Your code here
my_results <- c()
my_results <- c(my_results, 10)
print(my_results)
## [1] 10
c(1, 5, 2, 8, 4, NA), find the sum,
mean, and product of the vector. Then, do the same but ignore the
NA value.# Your code here
my_vector <- c(1, 5, 2, 8, 4, NA)
sum(my_vector)
## [1] NA
mean(my_vector)
## [1] NA
prod(my_vector)
## [1] NA
sum(my_vector, na.rm = TRUE)
## [1] 20
mean(my_vector, na.rm = TRUE)
## [1] 4
prod(my_vector, na.rm = TRUE)
## [1] 320
c(1, 5, 2, 8, 4), find the minimum and
maximum values.# Your code here
my_vector2 <- c(1, 5, 2, 8, 4)
min(my_vector2)
## [1] 1
max(my_vector2)
## [1] 8
c(1, 5, 2, 8, 4) in ascending and
descending order.# Your code here
my_vector3 <- c(1, 5, 2, 8, 4)
sort(my_vector3)
## [1] 1 2 4 5 8
sort(my_vector3, decreasing = TRUE)
## [1] 8 5 4 2 1
c(1, 5, 2, 8, 4).# Your code here
5 %in% c(1, 5, 2, 8, 4)
## [1] TRUE
6 %in% c(1, 5, 2, 8, 4)
## [1] FALSE