Coerce to data.table.

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

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

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

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

Arguments

x

An R object.

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.

...

Additional arguments to be passed to or from other methods.

Value

data.table.

Details

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 as.data.table()

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

  • DataFrame.

  • GenomicRanges.

S4 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.

See getClass("data.table") for details.

See also

Examples

data(DataFrame, GRanges, IRanges, package = "acidtest") ## DataFrame to data.table ==== x <- as(DataFrame, "data.table") x <- as.data.table(DataFrame) print(x)
#> rn sample01 sample02 sample03 sample04 #> 1: gene01 1 2 3 4 #> 2: gene02 5 6 7 8 #> 3: gene03 9 10 11 12 #> 4: gene04 13 14 15 16
## GenomicRanges to data.table ==== x <- as(GRanges, "data.table") x <- as.data.table(GRanges) print(x)
#> rn seqnames start end width strand geneID #> 1: ENSG00000000003 X 100627109 100639991 12883 - ENSG00000000003 #> 2: ENSG00000000005 X 100584802 100599885 15084 + ENSG00000000005 #> 3: ENSG00000000419 20 50934867 50958555 23689 - ENSG00000000419 #> 4: ENSG00000000457 1 169849631 169894267 44637 - ENSG00000000457 #> 5: ENSG00000000460 1 169662007 169854080 192074 + ENSG00000000460 #> geneName #> 1: TSPAN6 #> 2: TNMD #> 3: DPM1 #> 4: SCYL3 #> 5: C1orf112
## IRanges to data.table ==== x <- as(IRanges, "data.table") x <- as.data.table(IRanges) print(x)
#> rn start end width score #> 1: a 1 5 5 1 #> 2: b 10 14 5 2 #> 3: c 20 24 5 3