SomaLogic/SomaDataIO

Vertically binding `soma_adat` objects with `rbind()` strips attributes

MoemenEltobgy opened this issue · 1 comments

Hello,
I wanted to merge two adat files together. I mean just stacking the rows over each other in one data file. I used rbind() for that, but the combined data file has lost most of its attributes:
is_intact_attr(combined_data)
✖ Attributes has only 3 entries: 'names', 'row.names', 'class'
[1] FALSE
So, I was stuck in the step after that at which i needed to add clinical data file to the combined data file using the explanation you are providing in the read me. Any help would be appreciated.

Thanks,
Moemen

Hi @MoemenEltobgy ,

Yes, this is the expected behavior for rbind(), because it strips attributes of all "bound" objects in the process.

But as a workaround, you can use dplyr::bind_rows() instead which is kinder to object attributes.

There are a few things to be careful of before you go this route:

  1. be absolutely sure the variable (column) names are identical between the ADATs you're binding
  2. never bind ADATs from different versions of SomaScan
  3. be careful writing out the resulting soma_adat object because the header information will likely (cannot) represent ALL the soma_adat objects. Untested, but it likely takes the info from the first object.
adat <- system.file("extdata", "example_data10.adat",
                    package = "SomaDataIO", mustWork = TRUE) |> read_adat()
a <- head(adat, 5L)
b <- tail(adat, 5L)

combined <- dplyr::bind_rows(list(a, b))

is_intact_attr(combined)

is.soma_adat(combined)

all.equal(combined, adat)