type: Accessing the type of an object

typeR Documentation

Accessing the type of an object

Description

Get or set the type of an object.

Note that type and type<- are defined as S4 generic functions and what type means exactly (and what type() returns) depends on the objects for which the type and/or type<- methods are defined.

Usage

type(x)
type(x) <- value

## Methods defined in the BiocGenerics package:

## S4 method for signature 'vector'
type(x)
## S4 method for signature 'array'
type(x)
## S4 method for signature 'factor'
type(x)  # returns "character"
## S4 method for signature 'data.frame'
type(x)

## S4 replacement method for signature 'vector'
type(x) <- value
## S4 replacement method for signature 'array'
type(x) <- value

Arguments

x

Any object for which the type() getter or setter is defined. Note that objects will either: not support the getter or setter at all, or support only the getter, or support both the getter and setter.

value

The type to set on x (assuming x supports the type() setter). value is typically (but not necessarily) expected to be a single string (i.e. a character vector of length 1).

Details

On an ordinary vector, matrix, or array x, type(x) returns typeof(x).

On a data frame x where all the columns are ordinary vectors or factors, type(x) is semantically equivalent to typeof(as.matrix(x)). However, the actual implementation is careful to avoid turning the full data frame x into a matrix, as this would tend to be very inefficient in general.

Note that for a matrix-like or array-like object, type(x) returns the type of the elements in the object. See ?S4Arrays::type for more information.

Value

type(x) is expected to return the type of x as a single string i.e. as a character vector of length 1.

See Also

  • showMethods for displaying a summary of the methods defined for a given generic function.

  • selectMethod for getting the definition of a specific method.

  • type,ANY-method in the S4Arrays package for the default type method.

  • type,DataFrame-method in the S4Arrays package, and type,PairwiseAlignments-method in the pwalign package, for examples of specific type methods (defined for DataFrame and PairwiseAlignments objects, respectively).

  • BiocGenerics for a summary of all the generics defined in the BiocGenerics package.

Examples

type
showMethods("type")

`type<-`
showMethods("type<-")

## The BiocGenerics package defines methods for ordinary vectors, arrays,
## and data frames:
m <- matrix(11:22, nrow=3)
type(m)           # equivalent to 'typeof(m)' or 'storage.mode(m)'
type(m) <- "raw"  # equivalent to 'storage.mode(m) <- "raw"'
m
type(m)

selectMethod("type", "array")

selectMethod("type<-", "array")

df <- data.frame(a=44:49, b=letters[1:6], c=c(TRUE, FALSE))
stopifnot(identical(type(df), typeof(as.matrix(df))))

## Examples of methods defined in other packages:

library(S4Arrays)
showMethods("type")
selectMethod("type", "ANY")  # the default "type" method

library(pwalign)
showMethods("type")
## The type() method for PairwiseAlignments objects:
selectMethod("type", "PairwiseAlignments")

Bioconductor/BiocGenerics documentation built on Nov. 17, 2024, 6:52 p.m.