R - Vector

Card Puncher Data Processing

About

The Vector in R contains elements of the same class.

A Vector with different class of objects is a list.

A vector (as every object) can also have names.

Almost all data in R is a vector or is based upon vectors:

The elements of a vector in R have an explicit order, and each element can be individually indexed. R's in-memory processing relies on this order of elements for many computations, e.g., computing quantiles and summaries for time series objects.

Syntax

Constructor

vector(mode = "logical", length = 0)

where:

  • mode is the class
  • length is the number of element

Example of:

  • an empty vector (logical of length 1)
> v=vector()
> v
logical(0)
  • an integer vector of length 2
> x = vector(mode = "integer", length = 2)
> x
[1] 0 0
> str(x)
 int [1:2] 0 0

The [1] indicates that x is of the vector class

Combine

myVector <- c(object,object,...)  

where c is the combine function. The default method combines its arguments to form a vector.

Example:

> c(1,2)
[1] 1 2

Sequence

s = seq(from=1,to=4)
> s
[1] 1 2 3 4

Repeat

vector = rep(0,5)
[1] 0 0 0 0 0

Atomic vector types

There are 6 basic/atomic vector types:

  • logical: a boolean value (for example: TRUE)
  • integer: an integer value (for example: 1)
  • numeric (double): a real number (for example: 1.5)
  • character: a character string (for example: “foobar”)
  • complex: a complex number (for example: 1+2i)
  • raw: bytes

Class Hierarchy

The following class hierarchy occurs when creating an vector with several different object classes:

  1. NULL
  2. list
  3. expression

This operation is named: coercion.

Example:

> v = c(NULL, TRUE)
> class(v)
[1] "logical"
> v = c(TRUE, 2)
> class(v)
[1] "numeric"
> v = c(1, "Nico") 
> class(v)
[1] "character"

Attributes

Names

> v = 1:2
> names(v)
NULL
> names(v)=c("Nic","o")
> names(v)
[1] "Nic" "o"  
> v
Nic   o 
  1   2 

Operations

See R - (Mathematical|Logical) Operators

Many operations in R are vectorized.

> x=1:4;y=6:3
> x
[1] 1 2 3 4
> y
[1] 6 5 4 3
> z =2
> z
[1] 2
> w=c(2,4)
> w
[1] 2 4

Addition

# Addition
> x+y
[1] 7 7 7 7
> x+z
[1] 3 4 5 6
> x+w
[1] 3 6 5 8

Distinct / Unique

unique(x)

Others

# Multiplication
> x*y
[1]  6 10 12 12
# Division
> x/y
[1] 0.1666667 0.4000000 0.7500000 1.3333333
# Power
> x^y
[1]  1 32 81 64
# Logical
> x>2
[1] FALSE FALSE  TRUE  TRUE
> x>=2
[1] FALSE  TRUE  TRUE  TRUE
> y==1
[1] FALSE FALSE FALSE FALSE

How to

Subset

> v=1:5
> v
[1] 1 2 3 4 5
  • Indexing
> v[2]
[1] 2
  • Slicing
> v[3:5]
[1] 3 4 5
> v[5:3]
[1] 5 4 3
  • Condition
> v[v>3]
[1] 4 5
> c=v>3
> c
[1] FALSE FALSE FALSE  TRUE  TRUE
> v[c]
[1] 4 5
  • Condition with replace
v=1:5
v[v<3]=0
v
[1] 0 0 3 4 5

Remove missing values (NAs)

How to remove NAs value:

  • on one object
> v = c(1, 2, NA, 4, NA, 6)
> nas = is.na(v)
> nas
[1] FALSE FALSE  TRUE FALSE  TRUE FALSE
> v[nas]
[1] NA NA
> v[!nas]
[1] 1 2 4 6
  • with multiples objects where NA is not present in the column of all objects
> v = c(1, 2, NA, 4, NA, 6)
> v2 = c("Nico",NA,"Mad",NA,"Mel","Ri")
> notNa = complete.cases(v, v2)
> notNa
[1]  TRUE FALSE FALSE FALSE FALSE  TRUE
> v[notNa]
[1] 1 6
> v2[notNa]
[1] "Nico" "Ri"

Function

Length

> v <- c(1,-2,3.3,7,-3)
> length(v)
[1] 5

Str

Data Structure

> v <- c(1,-2,3.3,7,-3)
> str(v)
 num [1:5] 1 -2 3.3 7 -3

where

  • num: indicates the numeric class
  • [1:5]: from one to 5 elements
  • 1 -2 3.3 7 -3: the elements





Discover More
Card Puncher Data Processing
R - (Numeric|Double) Vector

Numeric vector for real numbers Generate 5 random numeric between 0 and 1. See Random in the normal distribution
Card Puncher Data Processing
R - (Object|Variable|Symbol)

R provides a number of specialized objects. They are created (instantiated), used and referenced through variable (known as symbol). When you read the term object in the documentation, you can interchange...
Card Puncher Data Processing
R - Character type

A vector containing character values. One List Concatenate vectors after converting to character. where: sep is the separator character between the arguments ...
Card Puncher Data Processing
R - Data Type

See: The 6 atomics vector types
Card Puncher Data Processing
R - Factor (Category, Enumerated Type)

The factor function is used to encode a vector as a factor (ie categorical data). When used with a numeric or a date, a binning function will return a factor. From numeric to a category (For instance,...
Card Puncher Data Processing
R - List

The list in R may contain elements of the different class (just like a data frame) class a vector (1 dimension) or a matrix (2 dimensions) data.tables and data.frames are internally lists with all...
Card Puncher Data Processing
R - Matrix

The matrix object in R is an array of two dimensions with the same class (data type). You can create a matrix with three methods: the matrix method the columns and rows bindings methods the...
Card Puncher Data Processing
R - Print

a vector with one value by line
Card Puncher Data Processing
R - Subset Operators (Extract or Replace Parts of an Object)

In the Data Manipulation category, you have the subset operators: [ [[ $ This operators acts on: vectors, matrices, arrays lists data.frame to extract or replace parts. See also:...
Card Puncher Data Processing
R - raw

A vector containing bytes



Share this page:
Follow us:
Task Runner