Hey there I have two vectors: y<- c(0.4, 0.0, 0.2, -0.2, -0.6, 0.2, 0.0, 0.0, 0.4, 0.4, 0.2) In the vector y, I want to access (in the order given) all of the values in between each of the specific values of given. I understand subsetting with y[i], but how do I get to ssomewhere in between -0.6 and 0.2? Thanks Eric Jennings matheric at myuw.net
Eric C. Jennings wrote:> Hey there > > I have two vectors: > > y<- c(0.4, 0.0, 0.2, -0.2, -0.6, 0.2, 0.0, 0.0, 0.4, 0.4, 0.2) > > In the vector y, I want to access (in the order given) all of the values in > between each of the specific values of given. > > I understand subsetting with y[i], but how do I get to ssomewhere in > between -0.6 and 0.2?Please give an example which value "somewhere in between -0.6 and 0.2" you want to get. Do you want to sample from some distribution, do you want the mean, do you want all values in steps of 1/1000 ???? Uwe Ligges> > Thanks > Eric Jennings > matheric at myuw.net > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>>>>> "Eric" == Eric C Jennings <matheric at u.washington.edu> >>>>> on Wed, 30 Nov 2005 21:39:12 -0800 writes:Eric> Hey there Eric> I have two vectors: Eric> y<- c(0.4, 0.0, 0.2, -0.2, -0.6, 0.2, 0.0, 0.0, 0.4, 0.4, 0.2) hmm, this is *one* vector , not two ! Eric> In the vector y, I want to access (in the order given) all of the values in Eric> between each of the specific values of given. Eric> I understand subsetting with y[i], but how do I get to ssomewhere in Eric> between -0.6 and 0.2? Though you could be much clearer, in your question, I'm pretty sure you are looking for *interpolation*; In that case, I'd recommend approx() or spline() -- or with same functionality but more elegant interface -- approxfun() and splinefun(). Martin Maechler, ETH Zurich
Eric C. Jennings wrote:> Hey there > > I have two vectors: > > y<- c(0.4, 0.0, 0.2, -0.2, -0.6, 0.2, 0.0, 0.0, 0.4, 0.4, 0.2) > > In the vector y, I want to access (in the order given) all of the values in > between each of the specific values of given. > > I understand subsetting with y[i], but how do I get to ssomewhere in > between -0.6 and 0.2? >Hi Eric, There was a function "filter" in the ts package, but that package seems to have disappeared. A Q&D function that would do what you want is: wapply<-function(x,window=2,FUN=mean,mode="numeric",...) { w1<-window-1 l1<-length(x)-w1 wout<-vector(mode,l1) for(i in 1:l1) wout[i]<-do.call(FUN,list(x=x[i:(i+w1)])) return(wout) } Jim
Eric C. Jennings wrote:> To start with, pardon my mistake regarding "two vectors" > > Yes I want all values (to two decimal digits if I can) between each of > the values given in the vector. > meaning I'm really try to do something like this: > y<- c(0.4 : 0.0 : 0.2 : -0.2 : 0.6 : 0.2 : 0.0 : 0.0 : 0.4 : 0.4 : 0.2) > which of course does not work. (But on this point, something like m<- > c(0.2:-0.6) doesn't work either. >Hi Eric, Okay, what you probably want is: seq(0.4,0.0,by=0.01*sign(0.0-0.4)) ... This can be worked into the function that I posted, but it's getting a bit tricky to make it general. What you want to do is to "walk" through the values creating a sequence between each one with the above call. in.betweens<-function(x,increment) { lenx<-length(x) newx<-x[1] for(i in 1:(lenx-1)) newx<-c(newx,seq(x[i],x[i+1],by=increment*sign(x[i+1]-x[i]))[-1]) return(zapsmall(newx)) } Jim