Description Usage Arguments Value Author(s) See Also Examples
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
.
1 | add_columns(x, columns)
|
x |
a |
columns |
a chracter vector of column names |
a data.frame
or same class as class(x)
Mark Cowley, 2012-09-04
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
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.