coerce: Coercion methods

Description Usage Arguments Details Value DataFrame (Bioconductor) coercion data.frame coercion data.table coercion tibble (tbl_df) coercion Note See Also Examples

Description

Coercion methods

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
as.DataFrame(x, ...)

as.data.frame(x, row.names = NULL, optional = FALSE, ...)

as.data.table(x, keep.rownames = FALSE, ...)

## S3 method for class 'DataFrame'
as_tibble(
  x,
  ...,
  rownames = pkgconfig::get_config("tibble::rownames", "rowname")
)

## S3 method for class 'IRanges'
as_tibble(
  x,
  ...,
  rownames = pkgconfig::get_config("tibble::rownames", "rowname")
)

## S3 method for class 'GenomicRanges'
as_tibble(
  x,
  ...,
  rownames = pkgconfig::get_config("tibble::rownames", "rowname")
)

## S3 method for class 'DataFrame'
as.data.table(x, keep.rownames = TRUE, ...)

## S3 method for class 'IRanges'
as.data.table(x, keep.rownames = TRUE, ...)

## S3 method for class 'GenomicRanges'
as.data.table(x, keep.rownames = TRUE, ...)

## S4 method for signature 'SimpleList'
as.DataFrame(x, row.names = NULL)

## S4 method for signature 'list'
as.DataFrame(x, row.names = NULL)

## S4 method for signature 'IRanges'
as.data.frame(x, row.names = NULL, optional = FALSE, ...)

## S4 method for signature 'Matrix'
as.data.frame(x, row.names = NULL, optional = FALSE, ...)

Arguments

x

any R object.

...

Additional arguments.

rownames

logical(1). Apply to row names.

keep.rownames

Default is FALSE. If TRUE, adds the input object's names as a separate column named "rn". keep.rownames = "id" names the column "id" instead.

row.names

NULL or character.

optional

logical. If TRUE, setting row names and converting column names (to syntactic names: see make.names) is optional. Note that all of R's base package as.data.frame() methods use optional only for column names treatment, basically with the meaning of data.frame(*, check.names = !optional). See also the make.names argument of the matrix method.

Details

These conversion methods are primarily intended to interconvert between popular tabular formats in R, including data.frame, data.table, tbl_df, and the Bioconductor DataFrame classes.

Value

Modified object, of desired conversion class.

DataFrame (Bioconductor) coercion

Don't define as() coercion method for list here. It will create issues with data.frame coercion. Use as.DataFrame() instead to coerce a list to DataFrame.

Wrapping the columns in an I() works when passing to DataFrame(). See also as_tibble() for easy list to data frame coercion.

as() method definition causes issues with data.frame to DataFrame coercion when defined, because data.frame inherits from list.

data.frame coercion

To IRanges

Default coercion of IPosRanges (i.e. IRanges) to data.frame currently strips metadata in mcols(). However, GenomicRanges preserves this information, so we're adding a tweaked coercion method here to improve consistency.

Relevant methods:

getMethod(
    f = "as.data.frame",
    signature = "GenomicRanges",
    where = asNamespace("GenomicRanges")
)
## IRanges inherits from `IPosRanges`.
getMethod(
    f = "as.data.frame",
    signature = "IPosRanges",
    where = asNamespace("IRanges")
)

See also:

data.table coercion

Our defined methods attempt to improve on the defaults in the data.table package to ensure that row names are not dropped by default, which is a poor default for bioinformatics. This is accomplished by setting keep.rownames = "rowname" by default instead of keep.rownames = NULL. Note that we're manually defining the "rowname" column instead of using TRUE, to match the conventions used in our as_tibble() methods.

S3 methods: as.data.table()

The package extends as.data.table() method support for these S4 classes:

S4 methods: as()

Since data.table is a class that extends data.frame, we need to define an S4 coercion method that allows us to use as() to coerce an object to a data.table.

tibble (tbl_df) coercion

Our defined methods attempt to improve on the defaults in the tibble package to ensure that row names are not dropped by default, which is a poor default for bioinformatics. This is accomplished by setting rownames = "rowname" by default instead of rownames = NULL.

Note that we're matching as_tibble() convention here, using rowname as column for row names assignment. We also using similar internal assert checks here, allowing atomic and/or list columns only.

S3 methods: as_tibble()

The package extends as_tibble() method support for these S4 classes:

S4 methods: as()

Since tbl_df is a virtual class that extends tbl and data.frame, we need to define an S4 coercion method that allows us to use as() to coerce an object to a tibble.

Note

Updated 2020-05-18.

See Also

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
data(
    DFrame,
    GRanges,
    IRanges,
    data.table,
    sparseMatrix,
    tbl_df,
    package = "AcidTest"
)

## `DataFrame` to `data.table` ====
x <- as(DFrame, "data.table")
x <- as.data.table(DFrame)
print(x)

## `DataFrame` to `tbl_df` ====
x <- as(DFrame, "tbl_df")
x <- as_tibble(DFrame)
print(x)

## `GenomicRanges` to `data.table` ====
x <- as(GRanges, "data.table")
x <- as.data.table(GRanges)
print(x)

## `GenomicRanges` to `tbl_df` ====
x <- as(GRanges, "tbl_df")
x <- as_tibble(GRanges)
print(x)

## `IRanges` to `data.table` ====
x <- as(IRanges, "data.table")
x <- as.data.table(IRanges)
print(x)

## `IRanges` to `tbl_df` ====
x <- as(IRanges, "tbl_df")
x <- as_tibble(IRanges)
print(x)

## `Matrix` to `DataFrame` ====
from <- sparseMatrix
to <- as(from, "DataFrame")
to

## `Matrix` to `data.frame` ====
x <- as(sparseMatrix, "data.frame")
head(x)

## `data.table` to `DataFrame` ====
from <- data.table
to <- as(from, "DataFrame")
head(to)

## `list` to `DataFrame` ====
## Use `as.DataFrame()` instead of `as()` for `list` class.
from <- list(
    a = list(c(1, 2), c(3, 4)),
    b = list(NULL, NULL)
)
to <- as.DataFrame(from)
to

## `tbl_df` to `DataFrame` ====
from <- tbl_df
to <- as(from, "DataFrame")
head(to)

acidgenomics/pipette documentation built on Sept. 27, 2021, 9:10 a.m.