Advertisement
Guest User

R and KDB Code for Pivot Table

a guest
Sep 15th, 2013
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
q/kdb+ 0.58 KB | None | 0 0
  1. R Code : (After bunzip2 t.csv.bz2)
  2.  
  3. z<-fread("~/q/m32/t.csv")
  4. ulist = unique(z$grp)
  5. timer=proc.time()
  6. z2 <- z[,list(sumcol=sum(col3)), by='col1,col2,grp'][,as.list((sumcol[match(ulist,grp)])), by=c("col1","col2")]
  7. proc.time() - timer
  8. setnames(z2[],c("col1","col2",ulist))
  9.  
  10.    user  system elapsed
  11.  27.215   0.503  27.759
  12.  
  13. Q Code: (this was used to create the t.csv file for use in R)
  14. q)n:5000000
  15. q)s:`a`b`c`d`e`f`g`h`i`j`k`l`m`n`o`p`q`r`s`t
  16. q)\t exec s#(grp!col4) by col1:col1,col2:col2 from update col4:sum col3 by col1,col2,grp from t
  17. 11543
  18. q)save `:t.csv
  19. `:t.csv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement