Search code examples
rr-s4

Removing S4 class column in R (flowCore)


I work with the package flowCore found in Bioconductor, which reads my data files in an S4 class format. You can type library(flowCore) and then data(GvHD) which loads an example dataset. When you type GvHD you can see that this dataset is made out of 35 experiments, which can be accessed individually by typing for example GvHD[[1]].

Now I am trying to delete two columns FSC-H and SSC-H from all the experiments, but I have been unsuccessful.

I have tried myDataSet<- within(GvHD, rm("FSC-H","SSC-H")) but it doesn't work. I would greatly appreciate any help.


Solution

  • I posted my question on the relevant GitHub page for flowCore and the answer was provided by Jacob Wagner.

    GvHD[[1]] is a flowFrame, not a simple data frame, which is why the NULL assignment doesn't work. The underlying representation is also a matrix, which also doesn't support dropping a column by assigning it NULL.

    If you want to drop columns, here are some ways you could do that. Note for all of these I'm subsetting columns for the whole flowSet rather than looping through each flowFrame. But you could perform these operations on each flowFrame as well.

    As Greg mentioned, choose the columns you want to keep:

    data(GvHD)
    all_cols <- colnames(GvHD)
    keep_cols <- all_cols[!(all_cols %in% c("FSC-H", "SSC-H"))]
    GvHD[,keep_cols]
    

    Or you could just filter in the subset:

    GvHD[,!colnames(GvHD) %in% c("FSC-H", "SSC-H")]

    You could also grab the numerical indices you want to drop and then use negative subsetting.

    # drop_idx <- c(1,2)
    drop_idx <- which(colnames(GvHD) %in% c("FSC-H", "SSC-H"))
    GvHD[,-drop_idx]