I created a user defined function that will search text for certain values and then return a different value. This works fine for each individual call, however, when I try to use it in Tidyverse, with mutate it doesn't work anymore. I get a Warning:
Warning message:
In if (grepl("Unique", textValue)) { : the condition has length > 1 and only the first element will be used
I'm guessing it has something to do with types and formats but not sure how to solve it.
# create fake data
P1 = c("Unique Claims", "Unique Records", "Spend Today", "Spend Yesterday", "% Returned", "% Claimed")
P2 = as.tibble(P1)
#create function
assignFormat <- function (textValue = as.character()) {
if (grepl("Unique", textValue) > 0) {
numFormat = "Comma"
} else if (grepl("Spend", textValue) > 0) {
numFormat = "Currency"
} else if (grepl("%", textValue, ) > 0 ) {numFormat = "Percent"}
else numFormat = "Other"
return(numFormat)
}
#test function - works fine
assignFormat("% of CLaims")
assignFormat("Unique Records")
assignFormat("Total Spend")
#doesn't work
P3 = P2 %>%
mutate(y = assignFormat(value))
Things I've tried: switching to grep using GREP in mutate directly - creates three vectors instead
Options and help are appreciated!