This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!

data.table merge using Matthew Dowle method

By: gkaupas on Apr 20th, 2012  |  syntax: R  |  size: 0.65 KB  |  views: 3  |  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. require(data.table);
  2. cat("K,N,S", "K1,1,A", "K2,2,B", "K3,3,C", file="f1.dat", sep="\n");
  3. cat("K,N,S", "K2,9,Z", "K3,,", file="f2.dat", sep="\n");
  4. dt1 <- data.table(read.table("f1.dat", sep=",", header=TRUE, stringsAsFactors=FALSE, na.strings=""), key=c("K"));
  5. dt2 <- data.table(read.table("f2.dat", sep=",", header=TRUE, stringsAsFactors=FALSE, na.strings=""), key=c("K"));
  6. tables();
  7. dt1;
  8. dt2;
  9. if (!identical(names(dt1), names(dt2)))
  10.     stop("Assumed for now. Can relax later if needed.");
  11. w = chmatch(dt2$K, dt1$K);
  12. for (i in 2:ncol(dt2)) {
  13.     nna = !is.na(dt2[[i]]);
  14.     set(dt1,w[nna],i,dt2[[i]][nna]);
  15. }
  16. dt1 = rbind(dt1, dt2[is.na(w)]);
  17. dt1;
clone this paste RAW Paste Data