Want more features on Pastebin? Sign Up, it's FREE!
Guest

R and KDB Code for Pivot Table

By: a guest on Sep 15th, 2013  |  syntax: q/kdb+  |  size: 0.58 KB  |  views: 85  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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
clone this paste RAW Paste Data