user1807096
user1807096

Reputation: 25

Splitting dataframe into equal number of groups, differing row sizes

I would like to split a data frame that has many variables, and I would like to group by a certain variable where that variable has differing number of rows. Here is the reproducible example:

y = data.frame(num = 1:4, sort_var = rep(c('hhh', 'jjj','iii','aaa'),4))
x = data.frame(num = 5:7, sort_var = rep(c('ddd', 'ccc','bbb'),2))

xy = rbind(x,y)
xy = xy[order(xy$num),]

I would like to make groups out of the num column, where each group is made by grouping num 1:2, 3:4, 5:6 and 7.

Thanks.

Upvotes: 0

Views: 1081

Answers (3)

Jake Burkhead
Jake Burkhead

Reputation: 6535

library(car)

sp <- recode(xy$num, "1:2=1; 3:4=2; 5:6=3; 7=4")
split(xy, sp)

$`1`
   num sort_var
7    1      hhh
11   1      hhh
15   1      hhh
19   1      hhh
8    2      jjj
12   2      jjj
16   2      jjj
20   2      jjj

$`2`
   num sort_var
9    3      iii
13   3      iii
17   3      iii
21   3      iii
10   4      aaa
14   4      aaa
18   4      aaa
22   4      aaa

$`3`
  num sort_var
1   5      ddd
4   5      ddd
2   6      ccc
5   6      ccc

$`4`
  num sort_var
3   7      bbb
6   7      bbb

Upvotes: 0

Ferdinand.kraft
Ferdinand.kraft

Reputation: 12819

Use this:

by(xy, as.integer((xy$num+1)/2), I)

Note that I've used function I to process each group, which simply replicates the result. But you can(should) change it.

Upvotes: 0

James
James

Reputation: 66834

> split(xy,(xy$num-1)%/%2)
$`0`
   num sort_var
7    1      hhh
11   1      hhh
15   1      hhh
19   1      hhh
8    2      jjj
12   2      jjj
16   2      jjj
20   2      jjj

$`1`
   num sort_var
9    3      iii
13   3      iii
17   3      iii
21   3      iii
10   4      aaa
14   4      aaa
18   4      aaa
22   4      aaa

$`2`
  num sort_var
1   5      ddd
4   5      ddd
2   6      ccc
5   6      ccc

$`3`
  num sort_var
3   7      bbb
6   7      bbb

Upvotes: 1

Related Questions