Coerce to 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.

  • GRanges.

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(df, gr, ir, package = "acidtest") ## DataFrame to data.table ==== x <- as(df, "data.table") x <- as.data.table(df) print(x)
#> rowname sample1 sample2 sample3 sample4 #> 1: ENSG00000000001 1 5 9 13 #> 2: ENSG00000000002 2 6 10 14 #> 3: ENSG00000000003 3 7 11 15 #> 4: ENSG00000000004 4 8 12 16
## GRanges to data.table ==== x <- as(gr, "data.table") x <- as.data.table(gr) print(x)
#> rowname 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(ir, "data.table")
#> Error in as(ir, "data.table"): no method or default for coercing “IRanges” to “data.table”
x <- as.data.table(ir) print(x)
#> start end width names #> 1: 1 5 5 a #> 2: 10 14 5 b #> 3: 20 24 5 c