Skip to content

Instantly share code, notes, and snippets.

@micstr
Created March 30, 2015 14:07
Show Gist options
  • Save micstr/dfc7df2226b468a2e0cd to your computer and use it in GitHub Desktop.
Save micstr/dfc7df2226b468a2e0cd to your computer and use it in GitHub Desktop.
Bind new rows to a log if columns added in R
# Test rbind
# Kudos to http://stackoverflow.com/questions/15014339/rbind-with-new-columns-and-data-table
# old log
dt.1 <- data.table(message = c("A-ok", "no", "yes"),
date = c("Feb", "Mar", "Mar"))
# new log more columns
dt.2 <- data.table(message = c("watch"),
rows.added = c(100),
date = c("april fool"))
#data.table::rbind(dt.1, dt.2, fill=TRUE)
dt.fin <- rbind(dt.1, dt.2, fill=TRUE)
# use new.dt ordering
setcolorder(dt.fin, names(dt.2))
if (ncol(dt.2) == ncol(dt.fin)) {
setcolorder(dt.fin, names(dt.2))
} else {
warning("new order not imposed as old log columns deleted")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment