collapse: Remove equal and adjacent components of a vector

Description Usage Arguments Value Author(s) Examples

View source: R/collapse.R

Description

This functions takes a vector and removes duplicate, neighbouring entries. Indices are preserved under this operation. If you encounter a sequence of equal values within a vector, the first one is preserved and its right neighbours are removed.

Usage

1

Arguments

vec

Vector of type double - The input vector.

order

Boolean - Value indicating wether resulting dataframe should be sorted in decreasing order (defaults to TRUE).

Value

A data frame with two columns. 1st column $values: The resulting vector. 2nd column $indices: The corresponding indices matching the input vector.

Author(s)

P.v.W. Crommelin

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
vec = c(1,1,3,4,5,6,7,7,7,7,7,8,9,10,9,8,7,6,5,4,4,4,4,3,2,1)
N = length(vec)
plot(x = 1:N,
     y = vec,
     type = "l",
     main = "demonstration")
points(x = 1:N,
       y = vec)
collapsedVec = collapse(vec = vec,
                        order = FALSE)
lines(x = collapsedVec$indices,
      y = collapsedVec$values,
      col = "red")
points(x = collapsedVec$indices,
       y = collapsedVec$values,
       col = "red")
legend("topright",
       leg = c("original vector","collapsed vector"),
       col = c("black","red"),
       lty = 1)

PhilippVWC/myBayes documentation built on Oct. 2, 2020, 8:25 a.m.