Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 821
aaaaa Avatar asked Mar 02 '23 09:03

aaaaa


2 Answers

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
like image 117
Sotos Avatar answered Mar 11 '23 11:03

Sotos


A base R option

lst <- split(df, ~two)
nmax <- max(sapply(lst, nrow))
do.call(
  cbind,
  lapply(
    lst,
    function(x) {
      k <- nrow(x)
      x[k + seq_len(nmax - k), ] <- NA
      x
    }
  )
)

gives

  a.one a.two a.three b.one b.two b.three
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
like image 44
ThomasIsCoding Avatar answered Mar 11 '23 10:03

ThomasIsCoding