[Bioc-devel] DataFrameList to Wide Format DataFrame

Hervé Pagès hp@ge@@on@g|thub @end|ng |rom gm@||@com
Fri Dec 17 18:04:29 CET 2021


On 16/12/2021 23:00, Dario Strbenac via Bioc-devel wrote:

> Hello,
>
> Ah, yes, the sample names should of course be in the rows - Friday afternoon error. In the question, I specified "largely the same set of features", implying that the overlap is not complete. So, the example below will error.
>
> DFL <- DataFrameList(X = DataFrame(a = 1:3, b = 3:1, row.names = LETTERS[1:3]),
>                       Y = DataFrame(b = 4:6, c = 6:4, row.names = LETTERS[20:22]))
> unlist(DFL)
> Error in .aggregate_and_align_all_colnames(all_colnames, strict.colnames = strict.colnames) :
>    the DFrame objects to combine must have the same column names

unlist() uses rbind() internally to combine the rows and rbind() wants to see the same columns in all the DataFrame to combines. combineRows() is a more flexible version of rbind() that was added in BioC 3.13:

   do.call(combineRows, unname(as.list(DFL)))

   # DataFrame with 6 rows and 3 columns

   #           a         b         c
   #   <integer> <integer> <integer>
   # A         1         3        NA
   # B         2         2        NA
   # C         3         1        NA
   # T        NA         4         6
   # U        NA         5         5
   # V        NA         6         4

If you want to discuss this further, please ask on the support site.

H.


>
> This is long but works:
>
> allFeatures <- unique(unlist(lapply(DFL, colnames)))
> DFL <- lapply(DFL, function(DF)
> {
>    missingFeatures <- setdiff(allFeatures, colnames(DF))
>    DF[missingFeatures] <- NA
>    DF
> })
> DFLflattened <- do.call(rbind, DFL)
>
> Is there a one-line function for it?
>
> --------------------------------------
> Dario Strbenac
> University of Sydney
> Camperdown NSW 2050
> Australia
> _______________________________________________
> Bioc-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/bioc-devel

-- 
Hervé Pagès

Bioconductor Core Team
hpages.on.github using gmail.com



More information about the Bioc-devel mailing list