简体   繁体   中英

Combination of Vectors in R of different Lengths

I have two vectors of zip codes (sites and customers). 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. I am currently using crossing() to do this in 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. 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:

expand.grid(x, y)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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