How to split a vector by equal and different number of elements in R?

R ProgrammingServer Side ProgrammingProgramming

To split a vector by equal and different number of elements, we can use split function along with rep function. The rep function will define the repetition of the divisions for equal as well as different number of elements. For example, if a vector say x contains fifty values then splitting of x with different number of elements as 20, 10, 10, 5, 5 this can be done by using the command split(x,rep(1:5,c(20,10,10,5,5))).

Example1

Live Demo

> x1<-rnorm(20)
> x1

Output

 [1]  1.30316414 -0.80488291  0.23170812 -0.07318560 -0.73388857 -0.85952329
 [7] -0.88713465 -0.26618866  1.45634603  0.31282735  1.39285785  0.32501145

[13] -1.72088389 -0.20699097 -0.37173907  0.03042574 -1.88779297 -1.49188883

[19]  1.76346672 -0.78819850

Example

> split(x1,rep(1:5,c(4,4,4,4,4)))

Output

$`1`
[1]  1.3031641 -0.8048829  0.2317081 -0.0731856
 
$`2`
[1] -0.7338886 -0.8595233 -0.8871347 -0.2661887
 
$`3`
[1] 1.4563460 0.3128273 1.3928578 0.3250114
 
$`4`
[1] -1.72088389 -0.20699097 -0.37173907  0.03042574
 
$`5`
[1] -1.8877930 -1.4918888  1.7634667 -0.7881985

Example2

Live Demo

> x2<-rnorm(80)
> x2

Output

 [1]  1.11014501 -0.30485929  1.19911840  1.01925016 -2.74900977 -0.65568943
 [7]  1.23454821 -0.91710842  2.25818571  0.11509990  0.60064320 -0.99898231
[13] -0.90873904  0.68738377  0.50206863  0.71867815 -0.17149018 -0.19056878
[19] -0.26320262  0.11085357 -0.87968483  1.10847267 -0.88684214  0.25501541
[25]  0.17070674  0.87421060 -0.51739525 -0.15134489 -0.84236650  0.50036499
[31] -1.07865023 -0.14798676  0.26203826  1.16376336 -0.98983205  2.49089629
[37]  0.29128935  1.27024917  0.49313043  0.90345654  0.36708891  1.16796991
[43] -0.82016835  0.30527505 -1.07100642  0.42140017  0.49116119 -1.70181435
[49]  0.85880415 -1.72676868  0.69970268  0.72310038 -0.55340423 -1.93115027
[55] -0.66841699  1.12353618  0.58422511  0.49507549 -0.48937123 -0.45051950
[61] -0.68090745  0.13435125 -0.51724884  1.24981341  0.13391069  0.12207344
[67]  0.20733945  0.74509319  0.31462239  1.07309023 -1.26986929  0.42222945
[73]  0.19691607 -0.15895825  0.39972349 -1.35150483  0.66675370 -0.05661676
[79]  0.64220780  0.20113633

Example

> split(x2,rep(1:8,c(10,10,10,10,10,10,10,10)))

Output

$`1`
 [1]  1.1101450 -0.3048593  1.1991184  1.0192502 -2.7490098 -0.6556894
 [7]  1.2345482 -0.9171084  2.2581857  0.1150999
 
$`2`
 [1]  0.6006432 -0.9989823 -0.9087390  0.6873838  0.5020686  0.7186782
 [7] -0.1714902 -0.1905688 -0.2632026  0.1108536
 
$`3`
 [1] -0.8796848  1.1084727 -0.8868421  0.2550154  0.1707067  0.8742106
 [7] -0.5173952 -0.1513449 -0.8423665  0.5003650
 
$`4`
 [1] -1.0786502 -0.1479868  0.2620383  1.1637634 -0.9898321  2.4908963
 [7]  0.2912894  1.2702492  0.4931304  0.9034565
 
$`5`
 [1]  0.3670889  1.1679699 -0.8201684  0.3052750 -1.0710064  0.4214002
 [7]  0.4911612 -1.7018144  0.8588042 -1.7267687
 
$`6`
 [1]  0.6997027  0.7231004 -0.5534042 -1.9311503 -0.6684170  1.1235362
 [7]  0.5842251  0.4950755 -0.4893712 -0.4505195
 
$`7`
 [1] -0.6809075  0.1343513 -0.5172488  1.2498134  0.1339107  0.1220734
 [7]  0.2073394  0.7450932  0.3146224  1.0730902
 
$`8`
 [1] -1.26986929  0.42222945  0.19691607 -0.15895825  0.39972349 -1.35150483
 [7]  0.66675370 -0.05661676  0.64220780  0.20113633

Example3

Live Demo

> x3<-rpois(100,5)
> x3

Output

[1]  3  7  3  1  6  2  7  6  7  7  3  4  4  7  2  3  4  9  5  8  6  3  5  4  4
[26]  5  9  7  2  4  5  6  7  5  4  6  5  5  7  5  2  2  6  3  6  5  2  5  3  6
[51]  4  5  6  0  9  4  4  3  4  5  2  7  4  9  4  7  7  2  6  5  8  4  4  2  9
[76]  3  7  8  5  3  4  5  6  7  4  7  7  6  4  6  1  7  7  4  6  5  5  5 11  1

Example

> split(x3,rep(1:5,c(10,25,25,20,20)))

Output

$`1`
 [1] 3 7 3 1 6 2 7 6 7 7
 
$`2`
 [1] 3 4 4 7 2 3 4 9 5 8 6 3 5 4 4 5 9 7 2 4 5 6 7 5 4
 
$`3`
 [1] 6 5 5 7 5 2 2 6 3 6 5 2 5 3 6 4 5 6 0 9 4 4 3 4 5
 
$`4`
 [1] 2 7 4 9 4 7 7 2 6 5 8 4 4 2 9 3 7 8 5 3
 
$`5`
 [1]  4  5  6  7  4  7  7  6  4  6  1  7  7  4  6  5  5  5 11  1
raja
Published on 05-Mar-2021 06:39:37
Advertisements