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 class 'matrix'
Append(x, values, after = NULL, rows = FALSE, names = NULL, ...)
# S3 method for class 'data.frame'
Append(x, values, after = NULL, rows = FALSE, names = NULL, ...)
# Default S3 method
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.96318175 0.71027405 1  6 0.6444698 0.8734174 0.0001036914
#> [2,] 0.04693501 0.88509093 2  7 0.9948324 0.5155856 0.2051797453
#> [3,] 0.16140918 0.97687532 3  8 0.3146626 0.8694593 0.9452204907
#> [4,] 0.93648743 0.03489964 4  9 0.8558820 0.8565226 0.2813335131
#> [5,] 0.97068136 0.43875727 5 10 0.5403377 0.3456832 0.8809342172
Append(x, values=1:10, after=2)
#>            [,1]       [,2] [,3] [,4]      [,5]      [,6]         [,7]
#> [1,] 0.96318175 0.71027405    1    6 0.6444698 0.8734174 0.0001036914
#> [2,] 0.04693501 0.88509093    2    7 0.9948324 0.5155856 0.2051797453
#> [3,] 0.16140918 0.97687532    3    8 0.3146626 0.8694593 0.9452204907
#> [4,] 0.93648743 0.03489964    4    9 0.8558820 0.8565226 0.2813335131
#> [5,] 0.97068136 0.43875727    5   10 0.5403377 0.3456832 0.8809342172

Append(x, values=1:10, after=2, names = c("X","Y"))
#>                            X  Y                                 
#> [1,] 0.96318175 0.71027405 1  6 0.6444698 0.8734174 0.0001036914
#> [2,] 0.04693501 0.88509093 2  7 0.9948324 0.5155856 0.2051797453
#> [3,] 0.16140918 0.97687532 3  8 0.3146626 0.8694593 0.9452204907
#> [4,] 0.93648743 0.03489964 4  9 0.8558820 0.8565226 0.2813335131
#> [5,] 0.97068136 0.43875727 5 10 0.5403377 0.3456832 0.8809342172
Append(x, values=1:10, after=2)
#>            [,1]       [,2] [,3] [,4]      [,5]      [,6]         [,7]
#> [1,] 0.96318175 0.71027405    1    6 0.6444698 0.8734174 0.0001036914
#> [2,] 0.04693501 0.88509093    2    7 0.9948324 0.5155856 0.2051797453
#> [3,] 0.16140918 0.97687532    3    8 0.3146626 0.8694593 0.9452204907
#> [4,] 0.93648743 0.03489964    4    9 0.8558820 0.8565226 0.2813335131
#> [5,] 0.97068136 0.43875727    5   10 0.5403377 0.3456832 0.8809342172

# 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