Open andkov opened 8 years ago
@andkov Here's an example of stacking/combining a list of data.frames. If I were doing it again, I'd probably use the .id
parameter in rbind_all()
, instead of this line: ds_list[[i]]$source_table <- block_names[i]
.
regex_table_name <- "^(\\w{3})(\\w{3})Blocks(\\d{1,2})$"
# ---- load-data ---------------------------------------------------------------
...
table_names <- RODBC::sqlTables(channel_access)$TABLE_NAME
block_names <- grep(regex_table_name, table_names, perl=T, value=T)
ds_list <- list()
for( i in seq_along(block_names) ) {
ds_list[[i]] <- RODBC::sqlFetch(channel_access, sqtable=block_names[i], stringsAsFactors=F)
ds_list[[i]]$source_table <- block_names[i]
}
rm(i, table_names, block_names)
# ---- tweak-data --------------------------------------------------------------
ds <- ds_list %>%
dplyr::rbind_all() %>%
dplyr::select_(
"id" = "ID",
"row_id" = "rowID",
"block_value" = "BlockValue",
"used" = "Used",
"source_table"
) %>%
dplyr::mutate(
record_id = seq_len(n()),
id = as.integer(id),
used = as.logical(used),
value_1 = sub(regex_table_name, "\\1", source_table),
value_2 = sub(regex_table_name, "\\2", source_table),
value_3 = as.integer(sub(regex_table_name, "\\3", source_table))
)
Notes during meeting
ds
- used outside functionsd
- used within the scope of a function