Search code examples
rdataframesplitrows

split data.frame in two based on one column values


Let's suppose I have a data.frame df as follows:

df=data.frame(one=c(1,2,3,4,5,6,7), 
two=c('a','a','a','b','b','b','b'), 
three=c(1123,33,5566,212,1,90,876))

I need to split df in two based on the values of column two, i.e. a and b.

Here is my desired output:

  one.x two.x three.x one.y two.y three.y
   1      a    1123     4     b      212 
   2      a      33     5     b        1
   3      a    5566     6     b       90
  NA     NA      NA     7     b      876

Thanks


Solution

  • Here is an idea using zoo::cbind.zoo,

    do.call(zoo::cbind.zoo, split(df, df$two))
    
    #  one.a two.a three.a one.b two.b three.b
    #1 1     a     1123    4     b     212    
    #2 2     a       33    5     b       1    
    #3 3     a     5566    6     b      90    
    #4 <NA>  <NA>  <NA>    7     b     876