简体   繁体   English

R中不同长度的向量组合

[英]Combination of Vectors in R of different Lengths

I have two vectors of zip codes (sites and customers).我有两个 zip 代码向量(站点和客户)。 I am trying to find the combination of pairs between the two vectors.我试图找到两个向量之间的对组合。

Hence, if sites is size 3 and customers is size 4...I would expect 12 combinations.因此,如果网站的大小为 3,而客户的大小为 4……我希望有 12 种组合。 I am currently using crossing() to do this in R.我目前正在使用 cross() 在 R 中执行此操作。

However, when I put in my actual data sites of size 20 and customers size 6057, the function returns 35,760 combinations when I expected 121,140 (6057*20) combinations.但是,当我放入大小为 20 和客户大小为 6057 的实际数据站点时,function 返回 35,760 个组合,而我预计会有 121,140 (6057*20) 个组合。 Does that mean that there were that many duplicated combinations and they were removed?这是否意味着有很多重复的组合并且它们被删除了?

my code is copied below.我的代码复制如下。 Thanks in advance.提前致谢。

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'])

Base R solution:基础 R 解决方案:

expand.grid(x, y)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM