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(rse, package = "acidtest") stopifnot(is(rse, "RangedSummarizedExperiment")) df <- SummarizedExperiment::colData(rse) gr <- SummarizedExperiment::rowRanges(rse) ## DataFrame to data.table ==== x <- as(df, "data.table") x <- as.data.table(df) print(x)
#> rowname condition #> 1: sample01 A #> 2: sample02 A #> 3: sample03 A #> 4: sample04 A #> 5: sample05 A #> 6: sample06 A #> 7: sample07 B #> 8: sample08 B #> 9: sample09 B #> 10: sample10 B #> 11: sample11 B #> 12: sample12 B
## GRanges to data.table ==== x <- as(gr, "data.table") x <- as.data.table(gr) print(x)
#> rowname seqnames start end width strand geneID #> 1: gene001 X 100627109 100639991 12883 - ENSG00000000003 #> 2: gene002 X 100584802 100599885 15084 + ENSG00000000005 #> 3: gene003 20 50934867 50958555 23689 - ENSG00000000419 #> 4: gene004 1 169849631 169894267 44637 - ENSG00000000457 #> 5: gene005 1 169662007 169854080 192074 + ENSG00000000460 #> --- #> 496: gene496 13 20567069 20691437 124369 + ENSG00000032742 #> 497: gene497 16 5033960 5087379 53420 + ENSG00000033011 #> 498: gene498 12 122471600 122501073 29474 - ENSG00000033030 #> 499: gene499 7 151212490 151227230 14741 - ENSG00000033050 #> 500: gene500 7 151232489 151238827 6339 + ENSG00000033100 #> geneName geneBiotype broadClass entrezID #> 1: TSPAN6 protein_coding coding 7105 #> 2: TNMD protein_coding coding 64102 #> 3: DPM1 protein_coding coding 8813 #> 4: SCYL3 protein_coding coding 57147 #> 5: C1orf112 protein_coding coding 55732 #> --- #> 496: IFT88 protein_coding coding 8100 #> 497: ALG1 protein_coding coding 56052 #> 498: ZCCHC8 protein_coding coding 55596 #> 499: ABCF2 protein_coding coding 10061 #> 500: CHPF2 protein_coding coding 54480