add_columns-methods: Add columns to a 'data.frame' or 'matrix'

Description Usage Arguments Value Author(s) See Also Examples

Description

Alter the columns within a data.frame or matrix, to columns. Note this can have a side effect of reordering, adding new, and excluding existing columns, such that the final result will the same columns in the same order as specified by columns.

Usage

1
add_columns(x, columns)

Arguments

x

a data.frame, ExpressionSet, more to come?

columns

a chracter vector of column names

Value

a data.frame or same class as class(x)

Author(s)

Mark Cowley, 2012-09-04

See Also

merge_tsv

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
if (require(datasets)) {
# data.frame method:
data(iris)
add_columns(head(iris), c(colnames(iris), "newColumn"))
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species newColumn
# 1          5.1         3.5          1.4         0.2  setosa        NA
# 2          4.9         3.0          1.4         0.2  setosa        NA
# 3          4.7         3.2          1.3         0.2  setosa        NA
# 4          4.6         3.1          1.5         0.2  setosa        NA
# 5          5.0         3.6          1.4         0.2  setosa        NA
# 6          5.4         3.9          1.7         0.4  setosa        NA

# matrix method 
mat <- as.matrix(iris[1:5,1:4])
add_columns(mat, c("Sepal.Length", "Sepal.Width", "newColumn", "Petal.Length"))
#   Sepal.Length Sepal.Width newColumn Petal.Length
# 1          5.1         3.5        NA          1.4
# 2          4.9         3.0        NA          1.4
# 3          4.7         3.2        NA          1.3
# 4          4.6         3.1        NA          1.5
# 5          5.0         3.6        NA          1.4
}

drmjc/mjcbase documentation built on May 15, 2019, 2:27 p.m.