【发布时间】:2019-08-15 19:19:50
【问题描述】:
假设我有两个向量(在下面的例子中,它们是 cycles 和 thedates),我想将它们之间的每对组合组合到一个列表中。下面的代码为我提供了我想要的输出inlist,但我认为它不是很有效。我用outer 尝试了不同的东西,但我无法成功。
library(lubridate)
library(data.table)
cycles <- c(1,2,10*1:24)
thedates <- seq(ymd('2016-08-13',tz='GMT'), ymd('2019-08-13',tz='GMT'),by='day')
inputs <- matrix(nrow=length(thedates)*length(cycles),ncol=2)
inputs[,1] <- rep(thedates,each=length(cycles))
inputs[,2] <- rep(cycles,length(thedates))
inlist <- lapply(1:nrow(inputs), function(x) c(inputs[x,1], inputs[x,2]))
【问题讨论】:
-
tidyr 包中的
crossing函数怎么样? stackoverflow.com/questions/43228379/… -
考虑一下来自 tinyverse 的
expand.grid,base,包。 -
仅供参考 - 如果您尝试使用 google 连接类型,这将被视为交叉或笛卡尔连接。
标签: r