Append elements to a number of various objects as vectors, matrices, data.frames and lists. In a matrix either rows or columns can be inserted at any position. In data frames any vectors can be inserted. values will be recycled to the necessary length.

Append(x, values, after = NULL, ...)

# S3 method for matrix
Append(x, values, after = NULL, rows = FALSE, names = NULL, ...)
# S3 method for data.frame
Append(x, values, after = NULL, rows = FALSE, names = NULL, ...)
# S3 method for default
Append(x, values, after = NULL, ...)

Arguments

x

object for the elements to be inserted

values

the elements to be inserted

after

a subscript, after which the values are to be appended. If it's missing the values will be appended after the last element (or column/row).

rows

logical, defining if vector should be added as row or as column. Default is column (rows=FALSE).

names

the dimension names for the inserted elements(s)

...

further arguments (not used here)

Details

The vector x will be recycled to a length of the next multiple of the number of rows (or columns) of the matrix m and will be inserted such that the first inserted row (column) has the index i. If the dimnames are given, they will be used no matter if the matrix m has already dimnames defined or not.

Value

An object containing the values in x with the elements of values appended after the specified element of x.

Author

Andri Signorell <andri@signorell.net>

See also

Examples

Append(1:5, 0:1, after = 3)    # the same as append
#> [1] 1 2 3 0 1 4 5

# Insert columns and rows
x <- matrix(runif(25), 5)

Append(x, values=1:10, after=2, names = c("X","Y"))
#>                          X  Y                                
#> [1,] 0.1650110 0.5934084 1  6 0.3177902 0.05270097 0.12578923
#> [2,] 0.5669098 0.7789706 2  7 0.1116802 0.98650870 0.96318175
#> [3,] 0.8980774 0.3977716 3  8 0.1010954 0.60417397 0.04693501
#> [4,] 0.5944723 0.8498828 4  9 0.8000977 0.14878272 0.16140918
#> [5,] 0.8316899 0.7418456 5 10 0.3799467 0.53876017 0.93648743
Append(x, values=1:10, after=2)
#>           [,1]      [,2] [,3] [,4]      [,5]       [,6]       [,7]
#> [1,] 0.1650110 0.5934084    1    6 0.3177902 0.05270097 0.12578923
#> [2,] 0.5669098 0.7789706    2    7 0.1116802 0.98650870 0.96318175
#> [3,] 0.8980774 0.3977716    3    8 0.1010954 0.60417397 0.04693501
#> [4,] 0.5944723 0.8498828    4    9 0.8000977 0.14878272 0.16140918
#> [5,] 0.8316899 0.7418456    5   10 0.3799467 0.53876017 0.93648743

Append(x, values=1:10, after=2, names = c("X","Y"))
#>                          X  Y                                
#> [1,] 0.1650110 0.5934084 1  6 0.3177902 0.05270097 0.12578923
#> [2,] 0.5669098 0.7789706 2  7 0.1116802 0.98650870 0.96318175
#> [3,] 0.8980774 0.3977716 3  8 0.1010954 0.60417397 0.04693501
#> [4,] 0.5944723 0.8498828 4  9 0.8000977 0.14878272 0.16140918
#> [5,] 0.8316899 0.7418456 5 10 0.3799467 0.53876017 0.93648743
Append(x, values=1:10, after=2)
#>           [,1]      [,2] [,3] [,4]      [,5]       [,6]       [,7]
#> [1,] 0.1650110 0.5934084    1    6 0.3177902 0.05270097 0.12578923
#> [2,] 0.5669098 0.7789706    2    7 0.1116802 0.98650870 0.96318175
#> [3,] 0.8980774 0.3977716    3    8 0.1010954 0.60417397 0.04693501
#> [4,] 0.5944723 0.8498828    4    9 0.8000977 0.14878272 0.16140918
#> [5,] 0.8316899 0.7418456    5   10 0.3799467 0.53876017 0.93648743

# append to a data.frame
d.frm <- data.frame("id"   = c(1,2,3),
                    "code" = c("AAA", "BBB", "CCC"),
                    "val"  = c(111, 222, 333))
z <- c(10, 20, 30)

Append(d.frm, z, after=2, names="ZZZ")
#>   id code ZZZ val
#> 1  1  AAA  10 111
#> 2  2  BBB  20 222
#> 3  3  CCC  30 333