[R] inserting elements in a list

j+rhelp@howard.fm j+rhelp at howard.fm
Tue Feb 18 21:36:03 CET 2003


On Tue, 18 Feb 2003 07:47:56 -0800 (PST), "Thomas Lumley"
<tlumley at u.washington.edu> said:
> history()
> a<-c(1,2,3,5,6,3)
> N<-length(a)
> threes<- a==3
> offset<- c(0,cumsum(threes)[-N])
> a[offset+(1:N)]<-a
> a[which(threes)+offset[threes]+1]<-7

> On Tue, 18 Feb 2003 j+rhelp at howard.fm wrote:
<...>
> > The problem is that it is placing the '7's in the correct place, but is
> > not shifting the right-hand side of the list across to make room.
> 
> The code does work, but you have to use all of it, not just that line.
> The *previous* line does the shifting.

D'oh! Of course!

Now that *is* a clever solution. :-)
-- 
  Jeremy Howard
  jhoward at fastmail.fm




More information about the R-help mailing list