A vector is a list of items that are of the same type. A vector is similar to a list in R, but not the same. A vector is a data structure. To combine the list of items to a vector, use the c() function and separate the items by a comma.
# Vector of characters/strings gemstones <- c("jade", "ruby", "sapphire", "amethyst", "topaz", "emerald") # Print gems gemstones
There are two types of vectors in R. These two below are from w3schools.com.
- Atomic vectors, of which there are six types: logical, integer, double, character, complex, and raw. Integer and double vectors are collectively known as numeric vectors.
- Lists, which are sometimes called recursive vectors because lists can contain other lists.
Every vector has two key properties, its type, and its length.
library(tidyverse) # gemstone gemid <- c(1:6) gemstones <- c("jade", "ruby", "sapphire", "amethyst", "topaz", "emerald") gemhard <- c(6.5, 9.0, 7.5, 7.0, 8.0, 7.75) gem <- data.frame(gemid, gemstones,gemhard) View(gem) gem2 <- gem %>% arrange(gemhard) # the default is ascending View(gem2) gem3 <- gem %>% arrange(desc(gemhard)) View(gem3) typeof(gem$gemstones) length(gem$gemstones) typeof(gem$gemhard)
The first typeof() function returns ‘character” and the length() function returns 6. The second typeof() function returns “double”.