SHARE
TWEET

data.table merge using Matthew Dowle method

gkaupas Apr 20th, 2012 15 Never
  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;
RAW Paste Data
Top