Skip to content

Instantly share code, notes, and snippets.

@YiLi225
Last active March 29, 2020 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YiLi225/fa2cbcdf7d914f17c8d0555e0af924ad to your computer and use it in GitHub Desktop.
Save YiLi225/fa2cbcdf7d914f17c8d0555e0af924ad to your computer and use it in GitHub Desktop.
Check the columns of the datasets
### check whether the column names of 3 datasets match up
columns = sapply(series_data_, colnames)
### !!! The code below certainly works for datasets with small numbers of columns,
### However, what if we have 1000 columns to do pair-wise checking,
### or additional columns being added to the datasource?
all(columns[, 1] == columns[, 2])
all(columns[, 2] == columns[, 3])
all(columns[, 1] == columns[, 3])
###========== the DRY principle: DON'T REPEAT YOURSELF
### let's do it in a smarter way :)
comb_cols = combn(seq(ncol(columns)), 2)
check_colnames <- function(x, y){
identical(columns[, x], columns[, y])
}
colnames_match = sapply(seq(ncol(comb_cols)), function(i) check_colnames(x = comb_cols[1, i], y = comb_cols[2, i]))
stopifnot(all(colnames_match))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment