rnullnanonblank

Adding two columns with blank entries as well getting all blank in final column


I am trying to add multiple columns ( in R) with many ( not all) blank entries but in final column I am getting all blanks. I am using simple +/- opertorfor this operation.

Final8$Abs_diff <- abs((Final8$prev_so_Qty + Final8$prev_dc_Qty + Final8$Import_Qty + 
                        Final8$Fs_trns_in_Qty) - 
                       (Final8$Trade_Qty + Final8$Fs_trns_out_Qty + Final8$crt_so_Qty + 
                        Final8$crt_dc_Qty))

I am felling that blanks entries are creating issue here, could someone help me how to avoid this situation


Solution

  • Use the function rowSums with na.rm = TRUE to handle the missing values after cbind the columns.

    Final8$Abs_diff <- abs(rowSums(cbind(Final8$prev_so_Qty, Final8$prev_dc_Qty,  Final8$Import_Qty,  
                            Final8$Fs_trns_in_Qty), na.rm = TRUE) - 
                           rowSums(cbind(Final8$Trade_Qty, Final8$Fs_trns_out_Qty, Final8$crt_so_Qty,  
                            Final8$crt_dc_Qty), na.rm = TRUE))