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]]
N=as.vector() This doesn't mean anything in R: you can read the help using ?as.vector to see what the required arguments are. Do you want to loop over the elements of data? That's not what your loop is doing. Instead it is looping over each element of the sequence from the min to the max *value* of data. It isn't actually doing anything with data (which btw is a bad thing to call your data, since it's also the name of a function, and really not very descriptive), just extracting the min and max and ignoring the actual values. Your loop is also only returning a single value. Presumably what you want is N <- rep(NA, lengthofdesiredresult) # I'm not sure what that is and to actually index N within the loop, ie N[i] <- somevalue You did a good job of providing fake data, thank you. But you also need to tell us what your expected result is, since it isn't clear from your code which parts are misunderstandings and which are intentional. Sarah On Mon, Oct 22, 2012 at 10:51 AM, Balqis <aehan3616 at 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! >-- Sarah Goslee http://www.functionaldiversity.org
thank you. yes I want to loop over the elements of the data. I want the output to be corresponded to the input by unchanging index. at the same time, the data that is more than c value should follow the function (a*(data-c)^0.5)+(b*(data-c), and the rest (same or less than c) should return a zero. I tried adjusting the code (which is still in ridiculous form and need some comments); fun<-function(a,b,c,data) { N=rep(NA,length(data)) for (i in 1:length(data)){ if(data>c){ N[i]<-(a*(data-c)^0.5)+(b*(data-c))} else {N[i]<-0} } return(N)} #try dummy y=c(100,210,320,130,170,120,220,90,55,45) try=fun(10000,0.2,150,data=y) On Mon, Oct 22, 2012 at 3:51 PM, Balqis <aehan3616@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]]
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 <aehan3616 at 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 at 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.
Then what about: fun<-function(a,b,c,data) { ifelse(data > c, (a*(data-c)^0.5)+(b*(data-c)), 0) } y=c(100,210,320,130,170,120,220,90,55,45) fun(10000,0.2,150,data=y)> fun(10000,0.2,150,data=y)[1] 0.00 77471.67 130418.05 0.00 44725.36 0.00 83680.00 [8] 0.00 0.00 0.00 Sarah On Mon, Oct 22, 2012 at 11:24 AM, Balqis <aehan3616 at gmail.com> wrote:> thank you. yes I want to loop over the elements of the data. I want the > output to be corresponded to the input by unchanging index. at the same > time, the data that is more than c value should follow the > function (a*(data-c)^0.5)+(b*(data-c), and the rest (same or less than c) > should return a zero. I tried adjusting the code (which is still in > ridiculous form and need some comments); > > fun<-function(a,b,c,data) > { > N=rep(NA,length(data)) > for (i in 1:length(data)){ > if(data>c){ > N[i]<-(a*(data-c)^0.5)+(b*(data-c))} > else > {N[i]<-0} > } > return(N)} > > #try dummy > y=c(100,210,320,130,170,120,220,90,55,45) > try=fun(10000,0.2,150,data=y) > > On Mon, Oct 22, 2012 at 3:51 PM, Balqis <aehan3616 at 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! >>-- Sarah Goslee http://www.functionaldiversity.org