简体   繁体   中英

Passing Vectors as Arguments in functions R

I would like to pass vectors as arguments in functions on R.

I want the function to have two Input vectors and output vector name.

Something like

ExampleFunction < - function(c(1,2),c(3,4),unionvector)
{
A<- firstvector
B<- SecondVector
unionvector <- A union B Vector
}

The result I want it to be like :- 1 2 inside the A vector 3 4 inside the B Vector & 1 2 3 4 inside the unionvector

I am not able to pass the Input vectors inside the arguments and can't get the ExampleFunction working.

You pass vector as arguments when making a call to the function. Use this:

exampleFunction<-function(A,B){
    return(unionvector=c(A,B))
}

exampleFunction(c(1,2),c(3,4))
#[1] 1 2 3 4

or

exampleFunction<-function(A=c(1,2),B=c(3,4)){
     return(unionvector=c(A,B))
 } 
exampleFunction()
#[1] 1 2 3 4

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