简体   繁体   中英

Create a function in R with multi outputs like matlab

I'm not familiar with R, just a newbie. So I want to translate some code from matlab to R. But I have the problem about the output of function. I want to create a function give output to two specify variable, like this:

list[a,b]<-function(var1,var2){
a<-var1 + var2
b<-var1 - var2
return list(a,b)
}

But my code is not working, please help me to solve this problem.

You seem to have some fundamental misunderstanding about functions in R. Read "An Introduction to R". Also, return is a function in R.

myfun <- function(var1, var2){
  a <- var1 + var2
  b <- var1 - var2
  return(list(a, b))
}

myfun(1:5, 10:6)
#[[1]]
#[1] 11 11 11 11 11
#
#[[2]]
#[1] -9 -7 -5 -3 -1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM