我认为这可以通过使用源代码的第一部分来完成,该部分执行单个收集操作。使用
iris
例如,这将生成600行输出,其中150行x 4列各一行
.
gatherpairs <- function(data, ...,
xkey = '.xkey', xvalue = '.xvalue',
ykey = '.ykey', yvalue = '.yvalue',
na.rm = FALSE, convert = FALSE, factor_key = FALSE) {
vars <- quos(...)
xkey <- enquo(xkey)
xvalue <- enquo(xvalue)
ykey <- enquo(ykey)
yvalue <- enquo(yvalue)
data %>% {
cbind(gather(., key = !!xkey, value = !!xvalue, !!!vars,
na.rm = na.rm, convert = convert, factor_key = factor_key),
select(., !!!vars))
} # %>% gather(., key = !!ykey, value = !!yvalue, !!!vars,
# na.rm = na.rm, convert = convert, factor_key = factor_key)%>%
# filter(!(.xkey == .ykey)) %>%
# mutate(var = apply(.[, c(".xkey", ".ykey")], 1, function(x) paste(sort(x), collapse = ""))) %>%
# arrange(var)
}