- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to split a vector into chunks in R?
This can be done with the help of seq_along, split, and ceiling.
Example
> x <- rpois(100,5) > x [1] 6 5 6 3 11 4 5 6 7 4 6 4 3 4 7 1 8 4 2 4 5 7 8 5 9 [26] 3 5 5 5 1 7 5 8 1 8 3 8 4 5 7 5 8 4 4 2 5 6 3 9 4 [51] 6 3 3 2 5 6 5 4 5 5 2 3 3 12 11 6 4 5 6 7 5 2 2 5 8 [76] 3 8 8 7 3 7 6 6 4 1 6 8 3 6 6 6 6 4 8 6 4 5 4 2 5 > max <- 20 > y <- seq_along(x) > chunks <- split(x, ceiling(y/max)) > chunks $`1` [1] 6 5 6 3 11 4 5 6 7 4 6 4 3 4 7 1 8 4 2 4 $`2` [1] 5 7 8 5 9 3 5 5 5 1 7 5 8 1 8 3 8 4 5 7 $`3` [1] 5 8 4 4 2 5 6 3 9 4 6 3 3 2 5 6 5 4 5 5 $`4` [1] 2 3 3 12 11 6 4 5 6 7 5 2 2 5 8 3 8 8 7 3 $`5` [1] 7 6 6 4 1 6 8 3 6 6 6 6 4 8 6 4 5 4 2 5
- Related Articles
- How to split a vector into smaller vectors of consecutive values in R?\n
- How to randomly split a vector into n vectors of different lengths in R?
- How do you split a list into evenly sized chunks in Python?
- How to split a long string into a vector of substrings of equal sizes in R?
- How to split a number into digits in R?
- How to convert a string vector into an integer vector in R?
- How to split comma separated values in an R vector?
- How to convert a vector into matrix in R?
- How to convert a vector into data frame in R?
- How to convert a vector into a diagonal matrix in R?
- How to convert a text vector into a matrix in R?
- How to split a string column into multiple columns in R?
- How to Split large string in to n-size chunks in JavaScript?
- How to a split a continuous variable into multiple groups in R?
- How to split a vector by equal and different number of elements in R?

Advertisements