Creating Vectors
A vector is a line of values of the same kind stored together, like a row of same-colored beads on a string. It is the most basic container in R.
In this page:
Syntax
vector_name <- c(value1, value2, value3)
vector_name <- 1:10
vector_name <- seq(from, to, by = step)
Creating Vectors
A vector is R's most fundamental data structure -- an ordered collection of values that all share the same type. Even a single value like x <- 5 is technically a vector of length 1.
Vectors are the building block that most of R's other data structures, like matrices and data frames, are built from.
Note:
Almost everything in R is a vector, even scalars -- there is no separate single-value type.
Example: Creating Vectors
x <- 5
y <- c(1, 2, 3, 4, 5)
cat(length(x), length(y), "\n")
print(y)
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Writing
y <- (1, 2, 3)withoutc, which is a syntax error. - Expecting a single value like
x <- 5to be different from a vector, when in R it is a vector of length 1. - Mixing types such as
c(1, "a")and being surprised that all elements become characters.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: