# Introduction to R programming 2: Vectors

1. Basic ways of creating vectors

2. Lengths and names of vectors

## 1 Basic ways of creating vectors

### 1.1 Colon operator (:)

> 1:5
[1] 1 2 3 4 5
> 7:-10
[1]   7   6   5   4   3   2   1   0  -1  -2  -3  -4  -5  -6  -7  -8
[17]  -9 -10
> 1.0:3.2
[1] 1 2 3
> 8.5:4.5
[1] 8.5 7.5 6.5 5.5 4.5
> 8.5:4.6
[1] 8.5 7.5 6.5 5.5 

### 1.2 The c() function

• The output type of c() function is determined from the highest type of the components in the following hierarchy:
NULL < raw < logical < integer < double < complex < character < list < expression

> c(1,1:3,c(5,8),13)
[1]  1  1  2  3  5  8 13
> c(1L,2,FALSE)
[1] 1 2 0
> c(1L,2,FALSE,'Hello')
[1] "1"     "2"     "FALSE" "Hello"
> c(1L,2,FALSE,'Hello',list(11,22))
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] FALSE

[[4]]
[1] "Hello"

[[5]]
[1] 11

[[6]]
[1] 22
• By default, the option recursive = FALSE. You can use recursive = TRUE to recursively descends through lists (and pairlists) combining all their elements into a vector.
> a=list(1,2,3)
> b=list(4,5,6)
> c(a,b)
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5

[[6]]
[1] 6

> c(a,b,recursive=FALSE)
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5

[[6]]
[1] 6

> c(a,b,recursive=TRUE)
[1] 1 2 3 4 5 6

### 1.3 The vector() function

The vector() function can be used to create dummy vectors, i.e. with elements being 'nothing', such as zero, FALSE, empty string, and so on.
Note that vector('Type_Name', n) is equivalent to Type_Name(n), except when the type is list.

> vector('numeric',3)
[1] 0 0 0
> numeric(5)
[1] 0 0 0 0 0
> vector('logical',3)
[1] FALSE FALSE FALSE
> logical(5)
[1] FALSE FALSE FALSE FALSE FALSE
> vector('double',3)
[1] 0 0 0
> double(5)
[1] 0 0 0 0 0
> vector('character',3)
[1] "" "" ""
> character(5)
[1] "" "" "" "" ""
> vector('list',3)
[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

> list(3)
[[1]]
[1] 3

As can be seen, list(3) creates a different object as vector(‘list’,3).

### 1.4 Creating vectors by sequence

Please visit Generating and Working with Sequence Data in R to learn how to use the seq() function. One important application of seq() function is to control a loop over elements of a vector.

> (Names<-c("Eric", "Sean", "Joseph"))
[1] "Eric"   "Sean"   "Joseph"
> seq(Names)
[1] 1 2 3
> for (i in seq(Names)) print(Names[i])
[1] "Eric"
[1] "Sean"
[1] "Joseph"
> length(Names)
[1] 3
> for (i in 1:length(Names)) print(Names[i])
[1] "Eric"
[1] "Sean"
[1] "Joseph"

> (Names<-character())
character(0)
> seq(Names)
integer(0)
> for (i in seq(Names)) print(Names[i])
> length(Names)
[1] 0
> for (i in 1:length(Names)) print(Names[i])
[1] NA
character(0)

As can be seen, even though both seq() and length() function can be used to loop over vectors, the former is more robust in suppressing loop over empty vector.

## 2 Lengths and names of vectors

• For character vectors, the length() function returns the number of string elements in the vector, while the nchar() function return how many characters in each string element of the vector.

> Names<-c("Eric", "Sean", "Joseph")
> length(Names)
[1] 3
> nchar(Names)
[1] 4 4 6 
• A vector can be truncated by assigning a new value to the length of a vector, but this is a somewhat uncommon (therefore non-recommended) thing to do.
> (x<-1:10)
[1]  1  2  3  4  5  6  7  8  9 10
> length(x)
[1] 10

> length(x)<-5
> x
[1] 1 2 3 4 5

> length(x)<-10
> x
[1]  1  2  3  4  5 NA NA NA NA NA
• A vector can be extended by assigning an element of non-existant indice. Note that NA is used to fill vacancy if necessary.
> (x<-1:10)
[1]  1  2  3  4  5  6  7  8  9 10
> length(x)
[1] 10

> length(x)<-5
> x
[1] 1 2 3 4 5

> length(x)<-10
> x
[1]  1  2  3  4  5 NA NA NA NA NA
> (Score1<-c(100,98,89))
[1] 100  98  89
> names(Score1)
NULL
> names(Score1)<-c("David Wang","Jack Ma","Eva Ding")
> Score1
David Wang    Jack Ma   Eva Ding
100         98         89
> names(Score1)
[1] "David Wang" "Jack Ma"    "Eva Ding"

> (Score2<-c("David Wang"=100, "Jack Ma"=98, "Eva Ding"=89))
David Wang    Jack Ma   Eva Ding
100         98         89 
Subject
Category
Coding language