Partition a vector into select groups with fixed length

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Partition a vector into select groups with fixed length

Bogaso
Hi,

Let say I have a vector as below

Vec = LETTERS

Now I want to break this vector into groups of the same length of 5.

So,
1st group consists - "A" "B" "C" "D" "E"
2nd group - "F" "G" "H" "I" "J"

and so on..
last group will consist only the leftover elements

I have a very large initial vector, so looking for some efficient way
to achieve the same. Any pointer will be highly appreciated.

Thanks for your time.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: Partition a vector into select groups with fixed length

Rui Barradas
Hello,

The following function will do it.
It uses a standard cumsum trick to create a break vector f.
And predicts special cases (n = 0 or n = 1).

breakVec <- function(x, n = 5){
   if(n < 1) stop(paste("Illegal value n:", n))
   if(n == 1){
     f = ""
   }else{
     f <- c(1, rep(0, n - 1))
     f <- rep(f, length.out = length(x))
     f <- cumsum(f)
   }
   split(x, f)
}

breakVec(LETTERS)
breakVec(LETTERS, 4)
breakVec(LETTERS, 1)
breakVec(LETTERS, -1)


Hope this helps,

Rui Barradas

Às 19:01 de 18/08/19, Christofer Bogaso escreveu:

> Hi,
>
> Let say I have a vector as below
>
> Vec = LETTERS
>
> Now I want to break this vector into groups of the same length of 5.
>
> So,
> 1st group consists - "A" "B" "C" "D" "E"
> 2nd group - "F" "G" "H" "I" "J"
>
> and so on..
> last group will consist only the leftover elements
>
> I have a very large initial vector, so looking for some efficient way
> to achieve the same. Any pointer will be highly appreciated.
>
> Thanks for your time.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: Partition a vector into select groups with fixed length

Gerrit Eichner
In reply to this post by Bogaso
Hi, Christofer,

try something along

len <- 5
split(Vec, rep(seq(ceiling(length(Vec)/len)), each = len))

  Hth  --  Gerrit

---------------------------------------------------------------------
Dr. Gerrit Eichner                   Mathematical Institute, Room 212
[hidden email]   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
Fax: +49-(0)641-99-32109            http://www.uni-giessen.de/eichner
---------------------------------------------------------------------

Am 18.08.2019 um 20:01 schrieb Christofer Bogaso:

> Hi,
>
> Let say I have a vector as below
>
> Vec = LETTERS
>
> Now I want to break this vector into groups of the same length of 5.
>
> So,
> 1st group consists - "A" "B" "C" "D" "E"
> 2nd group - "F" "G" "H" "I" "J"
>
> and so on..
> last group will consist only the leftover elements
>
> I have a very large initial vector, so looking for some efficient way
> to achieve the same. Any pointer will be highly appreciated.
>
> Thanks for your time.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: Partition a vector into select groups with fixed length

Bert Gunter-2
Perhaps simpler:
Hint:  (seq_along(LETTERS) -1) %/% 5
## modular arithmetic can be useful for this sort of thing


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sun, Aug 18, 2019 at 11:47 AM Gerrit Eichner <
[hidden email]> wrote:

> Hi, Christofer,
>
> try something along
>
> len <- 5
> split(Vec, rep(seq(ceiling(length(Vec)/len)), each = len))
>
>   Hth  --  Gerrit
>
> ---------------------------------------------------------------------
> Dr. Gerrit Eichner                   Mathematical Institute, Room 212
> [hidden email]   Justus-Liebig-University Giessen
> Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
> Fax: +49-(0)641-99-32109            http://www.uni-giessen.de/eichner
> ---------------------------------------------------------------------
>
> Am 18.08.2019 um 20:01 schrieb Christofer Bogaso:
> > Hi,
> >
> > Let say I have a vector as below
> >
> > Vec = LETTERS
> >
> > Now I want to break this vector into groups of the same length of 5.
> >
> > So,
> > 1st group consists - "A" "B" "C" "D" "E"
> > 2nd group - "F" "G" "H" "I" "J"
> >
> > and so on..
> > last group will consist only the leftover elements
> >
> > I have a very large initial vector, so looking for some efficient way
> > to achieve the same. Any pointer will be highly appreciated.
> >
> > Thanks for your time.
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> >
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: Partition a vector into select groups with fixed length

Rui Barradas
Hello,
Bert's modular arithmetic way simplifies my code a lot.

breakVec <- function(x, n = 5){
   if(n < 1) stop(paste("Illegal value n:", n))
   f <- if(n == 1) "" else (seq_along(x) - 1) %/% n
   split(x, f)
}



Hope this helps,

Rui Barradas

Às 20:08 de 18/08/19, Bert Gunter escreveu:

> Perhaps simpler:
> Hint:  (seq_along(LETTERS) -1) %/% 5
> ## modular arithmetic can be useful for this sort of thing
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Sun, Aug 18, 2019 at 11:47 AM Gerrit Eichner <
> [hidden email]> wrote:
>
>> Hi, Christofer,
>>
>> try something along
>>
>> len <- 5
>> split(Vec, rep(seq(ceiling(length(Vec)/len)), each = len))
>>
>>    Hth  --  Gerrit
>>
>> ---------------------------------------------------------------------
>> Dr. Gerrit Eichner                   Mathematical Institute, Room 212
>> [hidden email]   Justus-Liebig-University Giessen
>> Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
>> Fax: +49-(0)641-99-32109            http://www.uni-giessen.de/eichner
>> ---------------------------------------------------------------------
>>
>> Am 18.08.2019 um 20:01 schrieb Christofer Bogaso:
>>> Hi,
>>>
>>> Let say I have a vector as below
>>>
>>> Vec = LETTERS
>>>
>>> Now I want to break this vector into groups of the same length of 5.
>>>
>>> So,
>>> 1st group consists - "A" "B" "C" "D" "E"
>>> 2nd group - "F" "G" "H" "I" "J"
>>>
>>> and so on..
>>> last group will consist only the leftover elements
>>>
>>> I have a very large initial vector, so looking for some efficient way
>>> to achieve the same. Any pointer will be highly appreciated.
>>>
>>> Thanks for your time.
>>>
>>> ______________________________________________
>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>>>
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.