使用tidyverse
on R data.frames
,tidyr::unite
将是惯用的方式。
虽然不是dplyr
动词,但它还没有被翻译成通过dbplyr
/使用SQL
。
您可以在 SQL Server 中以这种方式定义自己的unite
(不幸的是我无法测试,但它应该可以工作):
unite.tbl <- function (data, col, ..., sep = "_", remove = TRUE)
{
dot_names <- sapply(substitute(list(...))[-1], deparse)
shown_cols <- if (remove)
setdiff(data$ops$vars, dot_names)
else data$ops$vars
shown_col_str <- paste(shown_cols, collapse = ", ")
concat_str <- paste0("CONCAT(",paste(dot_names, collapse = paste0(",'",sep,"',")),")")
col <- deparse(substitute(col))
subquery <- capture.output(show_query(data), type = "message")[-1] %>% paste(collapse = " ")
query <- paste("SELECT",shown_col_str,",",concat_str,"AS",col,"FROM (",subquery,")")
tbl(data$src$con, sql(query))
}
接着 :
tbl(channel,'##iris') %>%
unite(string,
Species, Sepal.Length, Sepal.Width, Petal.Length, Petal.Width,
sep = '',remove=FALSE)
对于支持||
连接运算符(例如Oracle
)的 DBMS,只需将concat_str
定义替换为:
concat_str <- paste(dot_names, collapse = paste0(" || '", sep, "' || "))