Try this -- this uses the vectorization which you need to read up on
in the Intro to R:

> #set the function
> fun<-function(a,b,c,data)
+ {
+ N <- numeric(length(data))  # initialize to zero
+ indx <- data > c  # determine which data is greater than c
+ N[indx] <- (a * (data[indx] - c) ^ 0.5) + (b * (data[indx] - c))
+ N
+ }
>
> #try dummy
> data=c(100,210,320,130,170,120,220,90,55,45)
> try=fun(10000,0.2,150,data=data)
>
> try
 [1]      0.00  77471.67 130418.05      0.00  44725.36      0.00
83680.00      0.00      0.00
[10]      0.00
>


On Mon, Oct 22, 2012 at 10:51 AM, Balqis <aehan3...@gmail.com> wrote:
> Hi all,
>
> I'm trying to create a function where it can process a vector and also give
> a vector output accordingly
> #input: a,b anc c are constants, data is the vector
>
> #set the function
> fun<-function(a,b,c,data)
> {
> N=as.vector()
> for (i in min(data):max(data)){
> if(i>c){
> N<-(a*(i-c)^0.5)+(b*(i-c))}
> else
> {N<-0}}
> return(N)
> }
>
> #try dummy
> data=c(100,210,320,130,170,120,220,90,55,45)
> try=fun(10000,0.2,150,data=data)
>
> what I get is:
> Error in as.vector() : argument "x" is missing, with no default
>
> Please help, thanks!
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to