   My Coding > Software > R > R vectors

# R vectors

One of the basic R data is vector. All elements in vector should be the same type.

## Initialization of the vector in R

We can create integer range using the colon : syntax. Range can be increasing or decreasing

``````
> v <- 1:10
> v
  1  2  3  4  5  6  7  8  9 10``````

We can specify exact values using the c function. We can also use all values from existing vector

``````
> w <-  c( 5, 10, 2, 99, v)
> w
  5 10  2 99  1  2  3  4  5  6  7  8  9 10``````

It is possible to use range in C function

``````
> x <- c(1:5, 10:15, 510, 520)
> x
   1   2   3   4   5  10  11  12  13  14  15 510 520``````

We can use function seq to specify some sequences. For example with small step, or fixes amount of steps

``````
> y <- seq( from=10, to=11, by=0.2 ) # step 0.2
> y
 10.0 10.2 10.4 10.6 10.8 11.0
> z <- seq( from=10, to=11, length.out=4 ) # 4 steps
> z
 10.00000 10.33333 10.66667 11.00000``````

### Initialization with key-value pair

We can initialize vector with key-value pairs.

``````
> s <- c('one'='oneoneone', 'two'='twotwotwo')
> s
one         two
"oneoneone" "twotwotwo"
> s['one']
one
"oneoneone" ``````

``````
> unname(s['one'])
 "oneoneone"``````

To see all names, we can use function names()

``````
> names(s)
 "one" "two"``````

using square brackets [ ] we can access to any data in our vector

``````
> y
 10``````

If we call element  we will receive type of data. We can also check data type by call typeof function

``````
> y
numeric(0)
> typeof(y)
 "double"``````

Three equivalent calls for range

>###CODE+###

w[1:3]

 5 10 2

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

 5 10 2

> w[c(1:3)]

 5 10 2

###CODE-###

Exclude one element for output – ve need to call it negatively. But element will not be deleted from the list

``````
> vv <- v[-2]
> vv
  1  3  4  5  6  7  8  9 10``````

### Boolean filtering

We can use boolean filtering in square brackets

``````
> even <- v[v%%2==0] ## can be divided by 2 without residue
> even
  2  4  6  8 10``````

or something more simple

``````
> big <- v[v > 8]
> big
  9 10``````

## Vector manipulating in R

Let’s create vectors for manipulating first

``````
> v <- 1:10 # 1 2 3 4 5 6 7 8 9 10
> w <- 10:1 # 10 9 8 7 6 5 4 3 2 1``````

Modify individual value

``````
> v <- 3
> v
  3  2  3  4  5  6  7  8  9 10``````

Modify all values which are obey some boolean filtering

``````
> w[w < 5] <- 0
> w
 10  9  8  7  6  5  0  0  0  0``````

Adding constant to vector (actually ve can do any mathematical operations)

``````
> z <- w + 2
> z
 12 11 10  9  8  7  2  2  2  2``````

Element wise operations + - / *

``````
> a1 <- w+v
> a1
 13 11 11 11 11 11  7  8  9 10
> a2 <- w / v
> a2
 3.3333333 4.5000000 2.6666667 1.7500000 1.2000000 0.8333333 0.0000000
 0.0000000 0.0000000 0.0000000``````

If you divide by 0 you will have inf value

``````
> a3 <- v / w
> a3
 0.3000000 0.2222222 0.3750000 0.5714286 0.8333333 1.2000000       Inf
       Inf       Inf       Inf``````

## Basic analysis of the vector content

There are few very important functions to work with vector:

• min() - min element
• max() - max element
• sum() - sum of all element
• prod() - multiplication of all elements
• mean() - man value
• length() - vector length
• sd() - standard deviation
• summary() - summary, universal function giving property of object

``````
> v <- 1:10
> min(v)
 1
> max(v)
 10
> sum(v)
 55
> prod(v)
 3628800
> mean(v)
 5.5
> length(v)
 10
> sd(v)
 3.02765``````

Summary() produce few most important object properties. And it is possible to access them as to vector with key-value pairs

``````
> v <- c(1, 4, 2, 3, 3, 1, 3, 5, 2, 3)
> m <- summary(v)
> m
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
1.0     2.0     3.0     2.7     3.0     5.0
> names(m)
 "Min."    "1st Qu." "Median"  "Mean"    "3rd Qu." "Max."
> m["Min."]
Min.
1
> unname(m["Min."])
 1``````

## Vector sorting

Vector can be sorted in R in Increasing by default or decreasing order

``````
> v <- c(1:5, 5:1)
> v
 1 2 3 4 5 5 4 3 2 1
> sort(v)
 1 1 2 2 3 3 4 4 5 5
> sort(v, decreasing=TRUE)
 5 5 4 4 3 3 2 2 1 1
String can also be sorted by this procedure, but this is case insensitive sorting
> w <- c("One", "two", "Three")
> sort(w)
 "One"   "Three" "two"  ``````

By default, sorting function sort() remove all NA values. To keep them ,you need to raise flag na.last=TRUE

``````
> u <- c(1, 3, NA, 2, 8, NA)
> sort(u)
 1 2 3 8
> sort(u, na.last=TRUE)
  1  2  3  8 NA NA``````

Published: 2021-11-09 06:51:49

### 9 popular artitles

© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit. 