Lisa
Lisa

Reputation: 959

How to split an ordered data frame into first half and second half multiple times in R

I would like to split my data frame into multiple halves based on the ordering of one column so that I can have a column containing "first half" and "second half". For example, I want the data to look like this:

1 - first half
2 - first half
3 - second half
4 - second half
2 - first half
3 - first half
4 - second half
5 - second half

Here is code to reproduce the basic structure the data

order <- data.frame(test = c(1:10, 4:13, 6:15))

and here is code to reproduce the desired output:

order$halves <- c(replicate(5, "firstHalf"), replicate(5, "secondHalf"), 
                  replicate(5, "firstHalf"), replicate(5, "secondHalf"), 
                  replicate(5, "firstHalf"), replicate(5, "secondHalf"))

I've seen this question about splitting a data frame, but I don't know how to do it multiple times.

Upvotes: 0

Views: 531

Answers (1)

Shree
Shree

Reputation: 11140

Here's a way in base R -

order$group <- rep(1:nrow(order), each = 5, length.out = nrow(order))

first_half <- order[order$group %% 2 == 1, ]
second_half <- order[order$group %% 2 == 0, ]

Output -

first_half
   test    halves group
1     1 firstHalf     1
2     2 firstHalf     1
3     3 firstHalf     1
4     4 firstHalf     1
5     5 firstHalf     1
11    4 firstHalf     3
12    5 firstHalf     3
13    6 firstHalf     3
14    7 firstHalf     3
15    8 firstHalf     3
21    6 firstHalf     5
22    7 firstHalf     5
23    8 firstHalf     5
24    9 firstHalf     5
25   10 firstHalf     5

second_half
   test     halves group
6     6 secondHalf     2
7     7 secondHalf     2
8     8 secondHalf     2
9     9 secondHalf     2
10   10 secondHalf     2
16    9 secondHalf     4
17   10 secondHalf     4
18   11 secondHalf     4
19   12 secondHalf     4
20   13 secondHalf     4
26   11 secondHalf     6
27   12 secondHalf     6
28   13 secondHalf     6
29   14 secondHalf     6
30   15 secondHalf     6

You can also use split(order, f = order$group %% 2 == 1) which generates a list of dataframes with each half.

Upvotes: 1

Related Questions