1

How to cut the values (1 to 100) in a regular interval (25) and place them into 4 groups as below:

sdr <- c(1:100)

Group1:  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Group2: 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

Group3: 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

Group4: 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

Any suggestion, please.

zx8754
  • 52,746
  • 12
  • 114
  • 209
DS1
  • 35
  • 3

2 Answers2

1

You could use split

sdr <- 1:100
split(sdr, rep(1:4, each = 25))
#$`1`
# [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#
#$`2`
# [1] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#
#$`3`
# [1] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#
#$`4`
# [1]  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94
#[20]  95  96  97  98  99 100

This returns a list with 4 vector elements.

Also note that the c() around 1:100 is not necessary.

Or we can define the number of groups

ngroup <- 4
split(sdr, rep(1:ngroup, each = length(sdr) %/% ngroup))

giving the same result.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
-2

You can make a dataframe for your groups and then transpose using t:

df <-  t(data.frame(Group1 = c(1:25), Group2 = c(26:50), Group3 = c(51:75), Group4 = c(76:100)))
kashiff007
  • 376
  • 2
  • 12