# R Notes: vectors

**F# and Data Mining**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

R is different from C family languages. It has a C syntax, but a Lisp semantics. Programmers from C/C++/Java world would find many usages in R adhoc and need to memorize special cases. This is because they use R from a C’s perspective. R is a very elegant language if we unlearn some C concepts and know R’s rules. I am writing several R notes to explain several important R language rules. This is the first note.

## The atomicity of R vectors

The **atomic** data structure in R is vector. This is so different from any C family language. In C/C++, built-in types such as `int `

and `char a`

re atomic data structures while C array (a continuous data block in memory) is obviously not the simplest type. In R, vector is indeed the most basic data structure. There is no scalar data structure in R – you cannot have a scalar `int`

in R as `int x = 10`

in C.

The atomicity of R vectors is written in many documents. The reason that it is usually skipped by R learners is that many R users come from C in which array is a composite data structure. Many seemingly special cases in R language all comes from the atomicity of R vectors. And I will try to cover them coherently.

### Display

x <- 10 # equivalent to x <- c(10)<br />x # or equivalent to print(x)<br />

## [1] 10<br />

y <- c(10, 20)<br />y<br />

## [1] 10 20<br />

What does `[1]`

mean in the output? It means that the output is a vector and from index 1, the result is `...`

x is a vector of length 1, so its value is `[1] 10`

, while y is a vector of length 2, so its value is `[1] 10 20`

. For a vector with longer length, the output contains more indices to assist human reading:

z <- 1:25<br />print(z)<br />

## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23<br />## [24] 24 25<br />

### Vectors with different types

Though vectors in R are atomic. There are different vectors: int vector, float vector, complex vector, character vector and logical vector. Int and float vectors are numeric vectors. In above, we have seen int vectors. Let’s see more types of vectors below:

x <- c(1, 2.1)<br />mode(x)<br />

## [1] "numeric"<br />

y <- c("a", "bb", "123")<br />mode(y)<br />

## [1] "character"<br />

z <- complex(real = 1, imaginary = 1)<br />mode(z)<br />

## [1] "complex"<br />

Notice that in R, string (In R’s term: character type) is like int, float, logical types. It is not a vector of chars. R does not differentiate between a character and a sequences of characters. R has a set of special functions such as `paste`

and `strsplit`

for string processing, however R’s character type is not a composite type and it is not a `vector`

of chars either!

### matrix and array

Matrix is a vector with augmented properties and this makes matrix an R class. Its core data structure is still a vector. See the example below:

y <- c(1, 2, 3, 4, 5, 6)<br />x <- matrix(y, nrow = 3, ncol = 2)<br />class(x)<br />

## [1] "matrix"<br />

rownames(x) <- c("A", "B", "C")<br />colnames(x) <- c("V1", "V2")<br />attributes(x)<br />

## $dim<br />## [1] 3 2<br />## <br />## $dimnames<br />## $dimnames[[1]]<br />## [1] "A" "B" "C"<br />## <br />## $dimnames[[2]]<br />## [1] "V1" "V2"<br />

x<br />

## V1 V2<br />## A 1 4<br />## B 2 5<br />## C 3 6<br />

as.vector(x)<br />

## [1] 1 2 3 4 5 6<br />

In R, arrays are less frequently used. A 2D arrays is indeed a matrix. To find more: `?array`

. We can say that an array/matrix is a vector (augmented with `dim`

and other properties). But we cannot say that a vector is an array. In OOP terminology, array/matrix is a subtype of vector.

### operators

Because the fundamental data structure in R is vector, all the basic operators are defined on vectors. For example, `+`

is indeed vector addition while adding two vectors with length 1 is just a special case.

When the lengths of the two vectors are not of the same length, then the shorter one is repeated to the same length as the longer one. For example:

x <- c(1, 2, 3, 4, 5)<br />y <- c(1)<br />x + y # y is repeated to (1,1,1,1,1)<br />

## [1] 2 3 4 5 6<br />

z <- c(1, 2)<br />x + z # z is repeated to (1,2,1,2,1), a warning is triggered<br />

## Warning: longer object length is not a multiple of shorter object length<br />

## [1] 2 4 4 6 6<br />

`+`

,`-`

,`*`

,`/`

,etc. are vector operators. When they are used on matrices, their semantics are the same when dealing with vectors – a matrix is treated as a long vector concatenated column by column. So do not expect all of them to work properly as matrix operators! For example:

x <- c(1, 2)<br />y <- matrix(1:6, nrow = 2)<br />x * y<br />

## [,1] [,2] [,3]<br />## [1,] 1 3 5<br />## [2,] 4 8 12<br />

For matrix multiplication, we shall use the dedicated operator:

x %*% y # 1 x 2 * 2 x 3 = 1 x 3<br />

## [,1] [,2] [,3]<br />## [1,] 5 11 17<br />

y %*% x # dimension does not match, c(1,2) is a row vector, not a col vector!<br />

## Error: non-conformable arguments<br />

The single-character operators are all operated on vectors and would expect generate a vector of the same length. So &, |, etc, are vector-wise logic operators. While `&&, ||, etc`

are special operators that generates a logic vector with length 1 (usually used in IF clauses).

x <- c(T, T, F)<br />y <- c(T, F, F)<br />x & y<br />

## [1] TRUE FALSE FALSE<br />

x && y<br />

## [1] TRUE<br />

### math functions

All R math functions take vector inputs and generate vector outputs. For example:

exp(1)<br />

## [1] 2.718<br />

exp(c(1))<br />

## [1] 2.718<br />

exp(c(1, 2))<br />

## [1] 2.718 7.389<br />

sum(matrix(1:6, nrow = 2)) # matrix is a vector, for row/col sums, use rowSums/colSums<br />

## [1] 21<br />

cumsum(c(1, 2, 3))<br />

## [1] 1 3 6<br />

which.min(c(3, 1, 2))<br />

## [1] 2<br />

sqrt(c(3, 2))<br />

## [1] 1.732 1.414<br />

`NA`

and `NULL`

NA is a valid value. NULL means empty.

print(NA)<br />

## [1] NA<br />

print(NULL)<br />

## NULL<br />

c(NA, 1)<br />

## [1] NA 1<br />

c(NULL, 1)<br />

## [1] 1<br />

*I find Knitr integrated with RStudio IDE is very helpful to write tutorials.

**leave a comment**for the author, please follow the link and comment on their blog:

**F# and Data Mining**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.