首页 > 解决方案 > R中不同长度向量的组合

问题描述

我有两个邮政编码向量(站点和客户)。我试图找到两个向量之间的对组合。

因此,如果网站的大小为 3,而客户的大小为 4……我希望有 12 种组合。我目前正在使用 cross() 在 R 中执行此操作。

但是,当我放入大小为 20 和客户大小为 6057 的实际数据站点时,该函数返回 35,760 个组合,而我期望有 121,140 (6057*20) 个组合。这是否意味着有很多重复的组合并且它们被删除了?

我的代码复制如下。提前致谢。

data <- read_xlsx("Sites.xlsx", sheet = "Sheet3")
data2 <- read_xlsx("Customers.xlsx", sheet = "Sheet1")

sites <- as.vector(data['FRT - Ship From Zip'])
sites

Customers <- as.vector(data2['Ship_To_Zip'])
Customers

Comdata <- crossing(Customers,sites)
Customers <- as.vector(Comdata['Ship_To_Zip'])
sites <- as.vector(Comdata['FRT - Ship From Zip'])

标签: rcombinations

解决方案


基础 R 解决方案:

expand.grid(x, y)

# Data
x <- 1:20
y <- 1:6057

推荐阅读