Last active
March 29, 2020 21:53
-
-
Save YiLi225/fa2cbcdf7d914f17c8d0555e0af924ad to your computer and use it in GitHub Desktop.
Check the columns of the datasets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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