Dear R users, I have a huge vector that I would like to split into unequal slices. However, the only way I can do this is to create another huge vector to define the groups that are used to split the original vector, e.g. # my vector is this a.vector <- seq(2, by=5, length=100) # indices where I would like to slice my vector cut.values <- c(30, 50, 100, 109, 300, 601, 803) # so I have to create another vector of similar length # to use the split() command, i.e. x <- rep(1:length(cut.values), times=diff(c(0, cut.values)) # this means I can use split() split(a.vector, x) This seems to be a waste in terms of memory usage as I'm creating another vector (here "x") to split the original vector. Is there a better way to split a huge vector than this? Any help is much appreciated. Best, Dave.
It's been a long day, so my brain isn't working too well! How about this: cut.values <- c(1, 30, 50, 100, 109, 300, 601, 803, length(a.vector)) vec.nms <- paste("vec",1:length(cut.values+1),sep="") for (i in 1:(length(cut.values)-1)) assign(vec.nms[i],a.vector[cut.values[i]:cut.values[i+1]]) One of the real experts, or just someone more wide awake, can probably come up with something better! -- ================================David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP
Hi On 20 Sep 2006 at 14:19, Dave Evens wrote: Date sent: Wed, 20 Sep 2006 14:19:04 -0700 (PDT) From: Dave Evens <devens8765 at yahoo.com> To: r-help at stat.math.ethz.ch Subject: [R] Spliting a huge vector> > Dear R users, > > I have a huge vector that I would like to split into > unequal slices. However, the only way I can do this is > to create another huge vector to define the groups > that are used to split the original vector, e.g. > > # my vector is this > a.vector <- seq(2, by=5, length=100)should not it be a.vector <- seq(2, by=5, length=803)> > # indices where I would like to slice my vector > cut.values <- c(30, 50, 100, 109, 300, 601, 803) > > # so I have to create another vector of similar length > # to use the split() command, i.e. > x <- rep(1:length(cut.values), times=diff(c(0, > cut.values))here it throws syntactic error so I assume it shall have one more parentheses> > # this means I can use split() > split(a.vector, x)then times <- diff(c(0,cut.values)) do.call(function(x, y, times) split(x,rep(y, times=times)), list(x=a.vector, y= cut.values, times=times)) or split(a.vector,rep(1:length(cut.values), times=times)) is this what you want? However I am not sure that some vector is not created internally. HTH Petr> > This seems to be a waste in terms of memory usage as > I'm creating another vector (here "x") to split the > original vector. Is there a better way to split a huge > vector than this? Any help is much appreciated. > > Best, > Dave. > > ______________________________________________ > 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 and provide commented, > minimal, self-contained, reproducible code.Petr Pikal petr.pikal at precheza.cz
Dear R users, Sorry, I made a mistake in my specification of problem, should be: # my vector is this a.vector <- seq(2, by=5, length=1000) # so my cut values are cut.values <- c(30, 50, 100, 109, 300, 601, 803, 1000) # and x should have an extra ")" at the end x <- rep(1:length(cut.values), times=diff(c(0, cut.values))) Thanks for all the responses so far. Any additional responses are welcome. Many thanks, Dave --- Dave Evens <devens8765 at yahoo.com> wrote:> > Dear R users, > > I have a huge vector that I would like to split into > unequal slices. However, the only way I can do this > is > to create another huge vector to define the groups > that are used to split the original vector, e.g. > > # my vector is this > a.vector <- seq(2, by=5, length=100) > > # indices where I would like to slice my vector > cut.values <- c(30, 50, 100, 109, 300, 601, 803) > > # so I have to create another vector of similar > length > # to use the split() command, i.e. > x <- rep(1:length(cut.values), times=diff(c(0, > cut.values)) > > # this means I can use split() > split(a.vector, x) > > This seems to be a waste in terms of memory usage as > I'm creating another vector (here "x") to split the > original vector. Is there a better way to split a > huge > vector than this? Any help is much appreciated. > > Best, > Dave. > > > __________________________________________________ > Do You Yahoo!?> protection around > http://mail.yahoo.com >