Description Usage Arguments Details See Also Examples
Convert a type of an object, such as vector
or matrix
or items contained in an object for
list
and data.frame
into a requested type.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
x |
any R object |
type |
R's (internal) storage type of object, such as |
The convert
function is a generalized methods::as()
and its variants as.character
,
as.numeric, etc., to work for all basic R types and not just vectors. The basic types implied here are
vector,
matrix,
data.frame. Unlike
methods::as', the object itself is not coerced
into a particular class, e.g, matrix won't become a list. Instead a numerical matrix might be
converted into a character matrix.
base::mode()
, base::typeof()
, methods::is()
, methods::as()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # For vectors, convert is identical to as
foo = 1:3
identical(convert(foo, "character"), as(foo, "character"))
# For matrices, convert is similar to mode, but a pure function
foo = matrix(1:4, 2, 2)
bar = foo
mode(bar) = "character"
identical(convert(foo, "character"), bar)
# For data.frames and lists, its convert is equivalent of converting all their items.
foo = data.frame("a"=1:2, "b"=c(0.1, 0.2))
bar = convert(foo, "character")
identical(foo$a, c("1","2"))
identical(foo$b, c("0.1", "0.2"))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.