convert: Convert object or its items into a requested type

Description Usage Arguments Details See Also Examples

View source: R/convert.r

Description

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.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
convert(x, type)

## Default S3 method:
convert(x, type)

## S3 method for class 'list'
convert(x, type)

## S3 method for class 'data.frame'
convert(x, type)

## S3 method for class 'matrix'
convert(x, type)

Arguments

x

any R object

type

R's (internal) storage type of object, such as "logical", "integer", "double", "complex", "character" or "raw".

Details

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.

See Also

base::mode(), base::typeof(), methods::is(), methods::as()

Examples

 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"))

bioDS/phyloRNA documentation built on Feb. 21, 2022, 3:28 p.m.