modifyData.R 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. }
  21. #' Map variable time OS status to status at cutoff
  22. #'
  23. #' @param df data frame
  24. #' @param cutoff time instance where OS is evaluated, same units as timeVar
  25. #' @param timeVar name of column where times of status evaluation are recorded (from treatment start)
  26. #' @param osVar name of column with status at evaluation (1-alive/progress free, 2-censored, 3-dead/w/disease)
  27. #' @param targetVar name of target variable holding status at cutoff (0-dead, 1-alive, 2-censored)
  28. #'
  29. #' @return updated data frame with targetVar
  30. selectValid<-function(df,cutoff=2,timeVar='years_to_event',osVar='st_osMAP',targetVar='osAtCutoff'){
  31. #if alive and ytoevent>c -> alive
  32. #if censored and ytoe>c -> alive
  33. #if dod and ytovent>c -> alive
  34. df[,targetVar]=1
  35. #if alive and ytoe<c -> censored
  36. df[(df[,timeVar]<cutoff) & df[,osVar] == 1,targetVar]=2
  37. #if censored and ytoe<c -> censored
  38. df[df[,timeVar]<cutoff & df[,osVar] == 2,targetVar]=2
  39. #if dod and ytovent<c -> dead
  40. df[df[,timeVar]<cutoff & df[,osVar] == 3,targetVar]=0
  41. df
  42. }