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]  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
 [1]  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]   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
[1] 10.0 10.2 10.4 10.6 10.8 11.0
> z <- seq( from=10, to=11, length.out=4 ) # 4 steps
> z
[1] 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" 

To access to individual value we need to use function unname()


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

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


> names(s)
[1] "one" "two"

Access to data

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


> y[1]
[1] 10

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


> y[0]
numeric(0)
> typeof(y)
[1] "double"

Three equivalent calls for range

>###CODE+###

w[1:3]

[1] 5 10 2

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

[1] 5 10 2

> w[c(1:3)]

[1] 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]  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
[1]  2  4  6  8 10

or something more simple


> big <- v[v > 8]
> big
[1]  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[1] <- 3
> v
 [1]  3  2  3  4  5  6  7  8  9 10

Modify all values which are obey some boolean filtering


> w[w < 5] <- 0
> w
 [1] 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
 [1] 12 11 10  9  8  7  2  2  2  2

Element wise operations + - / *


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

If you divide by 0 you will have inf value


> a3 <- v / w
> a3
 [1] 0.3000000 0.2222222 0.3750000 0.5714286 0.8333333 1.2000000       Inf
 [8]       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] 1
> max(v)
[1] 10
> sum(v)
[1] 55
> prod(v)
[1] 3628800
> mean(v)
[1] 5.5
> length(v)
[1] 10
> sd(v)
[1] 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)
[1] "Min."    "1st Qu." "Median"  "Mean"    "3rd Qu." "Max."   
> m["Min."]
Min. 
   1 
> unname(m["Min."])
[1] 1

Vector sorting

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


> v <- c(1:5, 5:1)
> v
 [1] 1 2 3 4 5 5 4 3 2 1
> sort(v)
 [1] 1 1 2 2 3 3 4 4 5 5
> sort(v, decreasing=TRUE)
 [1] 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)
[1] "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] 1 2 3 8
> sort(u, na.last=TRUE)
[1]  1  2  3  8 NA NA


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

Last 10 artitles


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.