1

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

kangaroo_cliff
  • 6,067
  • 3
  • 29
  • 42
Ashish
  • 115
  • 3
  • 15
  • Please add data using `dput` or something that we can copy and use. Read about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Oct 28 '20 at 04:01

1 Answers1

0

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))
kangaroo_cliff
  • 6,067
  • 3
  • 29
  • 42