Hi
I've written a pipe to clean up some of my data, however I'll need to rerun this several times, so I thought I'd make a function speed things up (speed being relative as I'm spending 99,9% of the time trying to figure out R..)
The pipe works as planned, feels a bit messy tho..?
mrgb_trus <- mrgb_trus %>%
mutate(MRGGG = str_replace_all(MRGB_gleason, c("3\\+3" = "1", "3\\+4" = "2",
"4\\+3" = "3", "4\\+4" = "4",
"4\\+5" = "5", "5\\+4" = "5",
"5\\+5" = "5"))) %>%
mutate(MRGGG = replace(MRGGG, is.na(MRGGG), 0)) %>%
mutate(MRGGG = replace(MRGGG, MRGB_gleason == "3" | MRGB_gleason == "4", "1")) %>%
mutate(MRGGG = as.numeric(as.character(MRGGG))) %>%
mutate(MRGGG = parse_factor(MRGGG, levels = GGG_levels))
However inserting this into a function does not work, two problems as far as I can tell:
testfunc <- function(df, old_col, new_col) {
GGG_levels <- c(0, 1, 2, 3, 4, 5)
df <- df %>%
mutate(new_col = str_replace_all(old_col, c("3\\+3" = "1", "3\\+4" = "2",
"4\\+3" = "3", "4\\+4" = "4",
"4\\+5" = "5", "5\\+4" = "5",
"5\\+5" = "5"))) %>%
mutate(new_col = replace(new_col, is.na(new_col), 0)) %>%
mutate(new_col = replace(new_col, old_col == "3" | old_col == "4", "1")) %>%
mutate(new_col = as.numeric(as.character(new_col))) %>%
mutate(new_col = parse_factor(new_col, levels = GGG_levels))
}
> testfunc(mrgb_trus, TRUS_G, TRUSGGG)
Error in mutate_impl(.data, dots) :
Evaluation error: object 'TRUS_G' not found.
This I "fixed" with selecting the column in the argument.
Then:
testfunc(mrgb_trus, mrgb_trus$TRUS_G, TRUSGGG)
Error in mutate_impl(.data, dots) :
Evaluation error: object 'TRUSGGG' not found.
How do I get the function to 1) use argument "old_col" to select a column in df without writing it every time, and 2) name the new column before the mutate has actually happened?
Thank you!