aline: Align a vector to match the certain length

Description Usage Arguments Value Author(s) Examples

Description

When the input vector is longer than the expected length, the vector will be truncated. When the input vector is shorter than the expected length, part or the whole body of it will be replcated to match the length. The overhead part will then also be truncated.

Usage

1
2
aline(x, length.out, rep.which = -1, rep.extend = c("times", "each"),
  append = NULL)

Arguments

x

input vector

length.out

numeric, the length of the expected result vector. It is consistent with length.out in rep()

rep.which

NULL or an integer vector, indicating the element index in x that will be replicated to extend the vector. Default -1. Only effective when append is NULL.

  • NULL: all the elements will be replicated.

  • integer vector (other than zero): the index of the elements to replicate. only the valid elements will be replicated (remove invalid indices).

    • positive: the index from top, e.g., 1, 2 in 1:10 means #1, #2.

    • negative: the index from bottom, e.g., -1, -2 in 1:10 means #10, #9.

rep.extend

character, the method to extend x. Default 'times'.

  • times: rep(x, times) will be called. When rep.which is not continuous, it is equivalent to each. When append is not NULL, append will be replicated using rep(append, times) to append to x.

  • each: rep(x, each) will be called. When append is not NULL, append will be replicated using rep(append, each) to append to x.

append

a vector for replication to append to x. Default NULL.

  • NULL: rep.which will be used to apply rep() to extend x.

  • others: append instead of rep.which will be used to apply rep() to extend x.

Value

a vector of length length.out

Author(s)

Yiying Wang, wangy@aetna.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## Not run: 
x <- letters[1:5]

aline(x, length.out=9, rep.which=2:3)
# [1] "a" "b" "c" "b" "c" "b" "c" "d" "e"

aline(x, length.out=9, rep.which=2:3, rep.extend='each')
# [1] "a" "b" "b" "b" "c" "c" "c" "d" "e"

aline(x, 9, rep.which=2:3, rep.extend='times', append=NA)
# [1] "a" "b" "c" "d" "e" NA  NA  NA  NA

aline(x, 9, rep.extend='each', append=c('m', 'n'))
# [1] "a" "b" "c" "d" "e" "m" "m" "n" "n"


y <- structure(1:5, names=letters[1:5])

aline(y, length.out=9, rep.which=2:3)
# a b c b c b c d e
# 1 2 3 2 3 2 3 4 5

aline(y, length.out=9, rep.which=c(2, 4))
# a b b b c d d d e
# 1 2 2 2 3 4 4 4 5

## End(Not run)

madlogos/aseskit documentation built on June 26, 2019, 12:17 a.m.