modifyData.R 590 B

1234567891011121314151617181920212223
  1. #' Sum columns in data frame df where some of the entries might be NA
  2. #'
  3. #' @param df data frame
  4. #' @param var1 first column
  5. #' @param var2 second column
  6. #' @param outVar which variable to store sum into
  7. #' @param valIfNA which value to use for NA
  8. #'
  9. #' @return updated data frame
  10. #' @export
  11. #'
  12. ## examples df<-sumWithNA(df,'lesionmtv41','metastasesmtv41','totalmtv41',0)
  13. sumWithNA<-function(df,var1='lesionmtv41',var2='metastasesmtv41',outVar='totalmtv41',valIfNA=0){
  14. v1=df[,var1]
  15. v2=df[,var2]
  16. v1[is.na(v1)]=valIfNA
  17. v2[is.na(v2)]=valIfNA
  18. df[,outVar]=v1+v2
  19. df
  20. }