Search notes:

R: atomic vectors

(Atomic) vectors are probably the most fundamental data structure in the R programming language.
An atomic vector is different from a one-dimensional array: an array has a dim attribute of length one while a vector has no such attribute.
Thus, dim() applied on a vector returns NULL.
An atomic vector is also different from a list. The elements of a vector are all of the same type while a list can contain elements of different and arbitrary types.
Atomic vectors are constructed with the c function or the vector function.
The three properties of vectors are

The six vector types

R has six basic («atomic») vector types:
typeof(…) mode(…) storage.mode(…)
logical logical logical
integer numeric integer
double numeric double
character character character
complex complex complex
raw raw raw

Homogeneous

All elements within a vector share the same type, that is: they have the same mode In contrast, lists are heterogeneous.
As vectors can have elements of only one data type, it's not possible to nest vectors within other vectors:
not_nested_vector <- c( 1, 1, 2, c(3, 5, 8), 13, 21);
not_nested_vector;
#
#  1  1  2  3  5  8 13 21
Github repository about-R, path: /data-structures/vector/not-nested.R

Creating vectors

For example with

1-indexed

The first element of a vector has index 1:
elems <- c('one', 'two', 'three')
elems[1] # one

Iterating over vectors

The apply function family is R's preferred way to call a function on each element of a vector or a list.

Comparing vectors

vec_one <- c(1, 2, 3)
vec_two <- c(0, 2, 4)

vec_one == vec_two
# [1] FALSE  TRUE FALSE

vec_one < vec_two
# [1] FALSE FALSE  TRUE

vec_one == 1
# [1]  TRUE FALSE FALSE
Github repository about-r, path: /vector/comparing.R

Selecting elements

vec <- 1:10

# Note: index is 1 based, not 0 based (as for example in
# Perl or C)
vec[4]
# [1] 4

vec[7:9]
# [1] 7 8 9

# Exclude specific elements (2, 4 and 6)
vec[c(-2,-4,-6)]
# [1]  1  3  5  7  8  9 10

# Select elements with TRUE (and excluding others with FALSE):
vec[c(FALSE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE)]
# [1]  2  5  6  7  8 10

# Same idea, but generating TRUE/FALSE
vec[vec <= 5]
# [1] 1 2 3 4 5

# Select all elements that are greater than the mean of the vector:
vec[vec > mean(vec)]
# [1]  6  7  8  9 10

# selecting elements by name
names(vec) <- c('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten')
vec[c('two', 'seven', 'three')]
#   two seven three
#     2     7     3
Github repository about-r, path: /vector/selecting_elements.R

Names property

Vectors can have the names property which names each element within the vector.
vec <- 1:5
names(vec) <- c('one', 'two', 'three', 'four', 'five')
vec
#   one   two three  four  five
#     1     2     3     4     5

vec[c('two', 'five')]
#  two five
#    2    5
Github repository about-r, path: /vector/names_property.R

Visualizing vectors

A simple method to visualize the values in a vector is to use barplot.

Printing vectors vertically

At times, the elements of a vector can be easier seen by printing each element on its own line. This is possible with cat(…).
vec <- c('foo', 'bar', 'baz');

cat(vec, sep="\n");
#
# foo
# bar
# baz
Github repository about-r, path: /data-structures/vector/print-vertically.R

See also

Vectors can be created with seq(…).
unlist(…) to turn a vector into a list.
R function: is.vector
The indices of a logical vector that are TRUE are returned by the function which().
match(…) returns the index of the first element in a vector that matches a specific value.
Vectorize(…).
In order to print each element of a vector in its own line, cat with sep="\n" might be used.
for allows to iterate over an atomic vector (See iterating over a character vector).
table can be used to count the frequencies of its elements and to create contigency tables from two or more vectors.
A vector can be turned into a factor with as.factor(…).
NA is a constant logical vector of length 1 whose only element is NA.
Cs of package Hmisc creates a character vector from unquoted names.
Create a matrix from a vector

Index