0

I have a function that takes a vector and returns some output.

my.function <- function(x){

   if (1 %in% x) {

     first.data <- data.frame(a = c(1, 2), b = c("a", "b"))
     return(first.data)    
   }

   if (2 %in% x){

   second.data <- data.frame(a = c("I", "II"), b = c("a", "b"))
   return(second.data) 
}}

my.function(x = c(1, 2))
  a b
  1 a
  2 b

Why does my function not return both first.data as well as second.data?

4
  • Do you mean lapply(c(1, 2), my.function)? Or Vectorize(my.function, SIMPLIFY = F) your function. Commented Aug 29, 2018 at 11:37
  • 7
    functions end after the first return has been done. Commented Aug 29, 2018 at 11:37
  • @missuse does it mean I can only have a single return command in a function and not multiple return? Commented Aug 29, 2018 at 11:41
  • 2
    You can have as many as you want/need, but understand that when the conditions for one of them have been meet the function will return the output without going forward. Commented Aug 29, 2018 at 11:42

3 Answers 3

3

You can simple call the function using lapply as follow:

lapply(1:2,my.function)

Output:

[[1]]
  a b
1 1 a
2 2 b

[[2]]
   a b
1  I a
2 II b

If you would like to follow your approach then:

 my.function <- function(x){
  for(i in 1:length(x)){ # This will call for each element in x
  if (1 %in% x) {
    first.data <- data.frame(a = c(1, 2), b = c("a", "b"))
    data = (first.data)    # Store intermediate result to data
  }

  if (2 %in% x){

    second.data <- data.frame(a = c("I", "II"), b = c("a", "b"))
    data=rbind(data,second.data) # Row wise bind the result
  }
    return(data) # Return the data
 }
}
my.function(x = c(1, 2))

Output:

    a b
 1  1 a
 2  2 b
 3  I a
 4 II b
Sign up to request clarification or add additional context in comments.

Comments

3

Whenever return statement executed, it won't execute next steps in a function. If you want to return both the outputs, use return to send the final output.

my.function <- function(x){
    first.data<-data.frame()
    second.data<-data.frame()
    if (1 %in% x) {
    first.data <- data.frame(a = c(1, 2), b = c("a", "b"))
    }

    if (2 %in% x){
        second.data <- data.frame(a = c("I", "II"), b = c("a", "b"))
    }
    return(list(first.data,second.data))
}

1 Comment

You mean do return(first.data) and return(second.data) in the end.
1

This should work:

my.function <- function(x){
first.data <- data.frame() 
second.data <- data.frame()

if (1 %in% x) {
 first.data <- data.frame(a = c(1, 2), b = c("a", "b"))   
}

if (2 %in% x){
   second.data <- data.frame(a = c("I", "II"), b = c("a", "b"))
}
list(first.data, second.data) # last element not declared is returned.
}

No need for return.

Keep in mind that if the if-statement is evaluated as FALSE, it will throw an error because the function won't find first.data or second.data.

Comments

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.