Creating a function in R with an argument whose inputs begin with a number -
i apologize if has been answered somewhere can't find question similar anywhere.
i trying create function 1 of input arguments (but isn't necessarily) string begins number. when try run function, r confused.
for example:
foo(df, 10, 5fum) gives me error:
error: unexpected symbol in "do_cv_class(data,10,5fum"
in function plan on taking "5fum" , parsing 2 arguments "5" , "fum". don't want create 4th argument separate 5 , fum.
any ideas? on great.
as comments above suggest, need pass "5fum" string. without rewriting r's parser, it's impossible think of way foo(df, 10, 5fum) legal input. presumably you're thinking this:
foo <- function(df, num1, str1) { str1_num <- as.numeric(gsub("([0-9]+)([[:alpha:]]+)","\\1",str1)) str1_alpha <- gsub("([0-9]+)([[:alpha:]]+)","\\2",str1) print(str1_num) print(str1_alpha) } foo(df, 10, "5fum") ## [1] 5 ## [1] "fum" (i thought passing argument ~5fum might work, ~ prevents subsequent symbols being evaluated immediately, doesn't: symbol 5fum still needs parsed.)
Comments
Post a Comment