2

I am trying to run the below simple function in R studio

    a<-testfunc15(state,outcome)
           {

            st<-state
            out<-outcome 
             print(st)
             tempdff<-healthcareoutcome[healthcareoutcome$State==st,]
             tempdff 

              }

When i copy and paste of the R prompt of the R studio ,i get an error :

    Error: could not find function "testfunc15"
     > {
      + 
      + st<-state
      + out<-outcome 
      + print(st)
      + tempdff<-healthcareoutcome[healthcareoutcome$State==st,]
      + tempdff 
       + 
       + }
        Error: object 'state' not found

when i try to source it :source("testfunc15.R") then i get this error :

      Error in eval(expr, envir, enclos) : could not find function"testfunc15"

I am saving the file in the same getwd() as other functions ,other functions are working fine. Where am i going wrong ? I couldn't find an answer on stackoverflow though there were many questions with the same description. Please help

2
  • 4
    Did you want the first line to be testfunc15 <- function(state,outcome){ Commented Feb 22, 2016 at 2:58
  • I got the same problem and my first line of code is correct I think? it's: mycustomfunction <- function (input1,input2,input3) { Commented Jun 24, 2021 at 9:31

1 Answer 1

2

The first line of your code is telling R to store the result of testfunc15(state, outcome) in a. It is not defining the function.

Also your function as written here doesn't exist. It should be, as mentionned by Dason, either :

testfunc15 = function (state, outcome) or testfunc15 <- function (state, outcome)

And the end could be return(tempdiff) or tempdiff

Sign up to request clarification or add additional context in comments.

10 Comments

The return is unnecessary, a function in R return the value of the last statement, return is only needed if you want to exit the function prematurely (in an if clause for example).
You can test it with a<- function(x) { x*2}; b <- a(5) and b should be 10 (on phone there may be typo)
While @Tensibai is correct, I'd argue that return is "necessary" for readability of the code. It's also necessary if you want to return a value that isn't derived from the last statement of the function. Example: f <- function() { dat <- data.frame(x = rnorm(100), y = rnorm(100)); write.csv(dat, path = "some/file.csv"); return(dat) }
@tensibai Fair enough. I've never really thought about the CPU time spent looking for return, but I can see how when running a function millions/billions of times, the consistent search for return is unnecessary.
And while at it, as the return value from <- is the assigned value, you can omit totally the last line in the function and end it with tmpdff <- tempdff<-healthcareoutcome[healthcareoutcome$State==st,] but this has few interest as it's local to the function, so just healthcareoutcome[healthcareoutcome$State==st,] would be enough. (and this avoid creating a copy in memory to free it just after)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.