rdataframer-s4isnan

Convert NA to 0


my data (typeof S4) is look like:

> rowData(ds_res1$res)
DataFrame with 144 rows and 4 columns
                                       cluster_id marker_id     p_val     p_adj
                                         <factor>  <factor> <numeric> <numeric>
B cells                  B cells                      OXO40  0.831005  0.996855
Activated Monocytes      Activated Monocytes          OXO40  0.664675  0.996855
NK cells CD56neg         NK cells CD56neg             OXO40  0.759837  0.996855
Unconventional monocytes Unconventional monocytes     OXO40  0.418055  0.996855
NK cells                 NK cells                     OXO40  0.709280  0.996855
...                                           ...       ...       ...       ...
Naïve CD4                              Naïve CD4      TIGIT        NA        NA
Memory CD9                             Memory CD9     TIGIT        NA        NA
gdT cells                              gdT cells      TIGIT        NA        NA
Undefined3                             Undefined3     TIGIT        NA        NA
Monocytes                              Monocytes      TIGIT  0.971238  0.996855

I am trying to convert the NA values in the dataframe to 0 and I have the below errors with two different approaches:

> rowData(ds_res1$res)[is.na(rowData(ds_res1$res))] <- 0
Error in mergeROWS(x, i, value) : appending gaps is not supported
In addition: Warning messages:
1: In NSBS(i, x, exact = exact, strict.upper.bound = !allow.append,  :
  subscript is an array, passing it thru as.vector() first
2: In NSBS(i, x, exact = exact, strict.upper.bound = !allow.append,  :
  subscript is an array, passing it thru as.vector() first

> rowData(ds_res1$res)[is.na(rowData(ds_res1$res)),] <- 0
Error in normarg_mcols(value, class(x), length(x)) : 
  trying to set metadata columns of length 575 on an object of length 144
In addition: Warning messages:
1: In NSBS(i, x, exact = exact, strict.upper.bound = !allow.append,  :
  subscript is an array, passing it thru as.vector() first
2: In NSBS(i, x, exact = exact, strict.upper.bound = !allow.append,  :
  subscript is an array, passing it thru as.vector() first
3: In `[<-.factor`(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,  :
  invalid factor level, NA generated
4: In `[<-.factor`(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,  :
  invalid factor level, NA generated

Solution

  • Not knowing what rowData is or how it is defined, my guess is that it does not have a LHS assignment companion function `rowData<-`. (For example, rownames(dat) has `rownames<-` so that we can do rownames(dat) <- c("a","quux").)

    In this case, if you want to fix NAs in the data returned from your rowData(..) call, then

    dat <- rowData(ds_res1$res)
    dat[] <- lapply(dat, function(z) { z[is.na(z)] <- 0; z; })
    

    While there's no hint that you're using dplyr, if you were then the canonical function is dplyr::coalesce, which can be used as:

    dat[] <- lapply(dat, dplyr::coalesce, 0)