> For the complete documentation index, see [llms.txt](https://r-pedia.gitbook.io/cevi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r-pedia.gitbook.io/cevi/r-data/data-structures/managing-vectors.md).

# --Managing Vectors

&#x20;A vector is a sequence of data elements of the same basic type: [numeric](http://uc-r.github.io/integer_double), [character](http://uc-r.github.io/characters), [logical](http://uc-r.github.io/logicals), [factors](http://uc-r.github.io/factors), or [dates](http://uc-r.github.io/dates) (exclude:  two additional vector types - complex and raw)

## #Creating Vectors

&#x20;Four main ways to create a vector: `:, c(), seq(), rep()`

```r
# integer vector
w <- 8:17
w
##  [1]  8  9 10 11 12 13 14 15 16 17

# double precision floating point (number with decimals) vector
x <- c(0.5, 0.6, 0.2)
x
## [1] 0.5 0.6 0.2

# logical vector
y <- c(TRUE, FALSE, FALSE)
y
## [1]  TRUE FALSE FALSE

# Character vector
z <- c("a", "b", "c") 
z
## [1] "a" "b" "c"
```

```r
# generate a sequence of numbers from 1 to 21 by increments of 2
seq(from = 1, to = 21, by = 2)             
##  [1]  1  3  5  7  9 11 13 15 17 19 21

# generate a sequence of numbers from 1 to 21 that has 15 equal 
# incremented 
# numbers
seq(0, 21, length.out = 15)    
##  [1]  0.0  1.5  3.0  4.5  6.0  7.5  9.0 10.5 12.0 13.5 15.0 16.5 
# 18.0 19.5
## [15] 21.0


# replicates the values in x a specified number of times in a 
# collated fashion
rep(1:4, times = 2)   
## [1] 1 2 3 4 1 2 3 4

# replicates the values in x in an uncollated fashion
rep(1:4, each = 2)    
## [1] 1 1 2 2 3 3 4 4
```

## #Coercing Vectors

&#x20;When you attempt to combine different types of elements (i.e. character and numeric) they will be coerced to the most flexible type possible:

```r
# numerics are turned to characters
str(c("a", "b", "c", 1, 2, 3))
##  chr [1:6] "a" "b" "c" "1" "2" "3"

# logical are turned to numerics...
str(c(1, 2, 3, TRUE, FALSE))
##  num [1:5] 1 2 3 1 0

# or character
str(c("A", "B", "C", TRUE, FALSE))
##  chr [1:5] "A" "B" "C" "TRUE" "FALSE"
```

```r
class(state.region)
## [1] "factor"

state.region2 <- as.character(state.region)
class(state.region2)
## [1] "character"
```

## #Adding on to Vectors

```r
v1 <- 8:17

c(v1, 18:22)
##  [1]  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22

# same as
c(v1, c(18, c(19, c(20, c(21:22)))))
##  [1]  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
```

## #Adding Attributes to Vectors

```r
attributes(v1)
## NULL
```

```r
# assigning names to a pre-existing vector
names(v1) <- letters[1:length(v1)]
v1
##  a  b  c  d  e  f  g  h  i  j 
##  8  9 10 11 12 13 14 15 16 17
attributes(v1)
## $names
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

# adding names when creating vectors
v2 <- c(name1 = 1, name2 = 2, name3 = 3)
v2
## name1 name2 name3 
##     1     2     3
attributes(v2)
## $names
## [1] "name1" "name2" "name3"
```

```r
comment(v1) <- "This is a comment on a vector"
v1
##  a  b  c  d  e  f  g  h  i  j 
##  8  9 10 11 12 13 14 15 16 17
attributes(v1)
## $names
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
## 
## $comment
## [1] "This is a comment on a vector"
```

## #Subsetting Vectors

* [Positive integers](http://uc-r.github.io/vectors#vector_positive)
* [Negative integers](http://uc-r.github.io/vectors#vector_negative)
* [Logical values](http://uc-r.github.io/vectors#vector_logical)
* [Names](http://uc-r.github.io/vectors#vector_names)

#### Subsetting with positive integers: <a href="#vector_positive" id="vector_positive"></a>

```r
v1
##  a  b  c  d  e  f  g  h  i  j 
##  8  9 10 11 12 13 14 15 16 17

v1[2]
## b 
## 9

v1[2:4]
##  b  c  d 
##  9 10 11

v1[c(2, 4, 6, 8)]
##  b  d  f  h 
##  9 11 13 15

# note that you can duplicate index positions
v1[c(2, 2, 4)]
##  b  b  d 
##  9  9 11
```

#### Subsetting with negative integers: <a href="#vector_negative" id="vector_negative"></a>

```r
v1[-1]
##  b  c  d  e  f  g  h  i  j 
##  9 10 11 12 13 14 15 16 17

v1[-c(2, 4, 6, 8)]
##  a  c  e  g  i  j 
##  8 10 12 14 16 17

```

#### Subsetting with logical values: <a href="#vector_logical" id="vector_logical"></a>

```r
v1[c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE)]
##  a  c  e  f  g  j 
##  8 10 12 13 14 17

v1[v1 < 12]
##  a  b  c  d 
##  8  9 10 11

v1[v1 < 12 | v1 > 15]
##  a  b  c  d  i  j 
##  8  9 10 11 16 17

# if logical vector is shorter than the length of the vector being
# subsetted, it will be recycled to be the same length
v1[c(TRUE, FALSE)]
##  a  c  e  g  i 
##  8 10 12 14 16
```

#### Subsetting with names: <a href="#vector_names" id="vector_names"></a>

```r
v1["b"]
## b 
## 9

v1[c("a", "c", "h")]
##  a  c  h 
##  8 10 15
```

#### Simplifying vs. Preserving: <a href="#vector_simplify" id="vector_simplify"></a>

&#x20;**Simplifying** subsets returns the simplest possible data structure that can represent the output. **Preserving** subsets keeps the structure of the output the same as the input.

```r
v1[1]
## a 
## 8

v1[[1]]
## [1] 8
```

## #Exercises

1. Check out the built-in character vector `state.name`.
2. How many elements are in this vector?
3. What attributes does this vector have?
4. Can you name each vector element with “V1”, “V2”, …, “V50” (shortcut: `paste0("v", 1:50)`)?
5. Now what attributes does this vector have?
6. Subset `state.name` for those elements with the following names: V35, V17, V14, V38.
