|
@@ -51,3 +51,37 @@ selectValid<-function(df,cutoff=2,timeVar='years_to_event',osVar='st_osMAP',targ
|
|
|
df
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+#' Remap/change value of variable according to a lookup table
|
|
|
+#'
|
|
|
+#' @param df data frame
|
|
|
+#' @param origVar name of original variable
|
|
|
+#' @param mapVar name of variable with updated values
|
|
|
+#' @param valueMap mapping of variables from old to new, named list
|
|
|
+#' where names() are values to be found in data frame and values() contain new values
|
|
|
+#' @param num ensure output value is numeric
|
|
|
+#'
|
|
|
+#' @return data frame with updated column
|
|
|
+#' @export
|
|
|
+
|
|
|
+remapVariable <- function(df, origVar, mapVar, valueMap, num=TRUE) {
|
|
|
+ if (!origVar %in% colnames(df)) {
|
|
|
+ base::stop(base::sprintf("Variable '%s' not found in data frame", origVar))
|
|
|
+ }
|
|
|
+ if (num){
|
|
|
+ # Ensures output type consistency
|
|
|
+ df[[mapVar]] <- base::vapply(base::as.character(df[[origVar]]), getNewValue, map=valueMap,FUN.VALUE = numeric(1))
|
|
|
+ }
|
|
|
+ else
|
|
|
+ df[[mapVar]] <- base::sapply(df[[origVar]], getNewValue, map=valueMap)
|
|
|
+
|
|
|
+ df
|
|
|
+}
|
|
|
+
|
|
|
+getNewValue<-function(x,map){
|
|
|
+ if (x %in% base::names(map))
|
|
|
+ map[[x]]
|
|
|
+ else
|
|
|
+ NA
|
|
|
+}
|