Reputation: 2018
my_function <- function(A,B,C,D,E) {
for (event in c(A,B,C,D,E)){
if (event %in% c(A,E)) print(paste0(event, "foo"))
if (event %in% c(B,C,D)) print(paste0(event, "bar"))
}
}
my_function(45, 34, 23, 213, 134)
[1] "45foo"
[1] "34bar"
[1] "23bar"
[1] "213bar"
[1] "134foo"
The function returns 5 character vectors of length 1. I would like the function to return one vector of length 5 - in the order passed by in the arguments (A>B>C>D>E)
Upvotes: 0
Views: 52
Reputation: 2141
Your function actually doesn't return anything, it just prints the statements. If you want to return something, this is an option:
my_function <- function(A,B,C,D,E) {
return(sapply(c(A,B,C,D,E),function(x){
if(x %in% c(A,E)) return(paste0(x,"foo"))
if(x %in% c(B,C,D)) return(paste0(x,"bar"))
}))
}
Upvotes: 1