首页 > 解决方案 > R中的数据结构分组数据对

问题描述

我在为流程组织数据时遇到问题。要求是我们有 N 艘拖船(用于运输大船的船)和 N 艘要由这些拖船到达的船。每艘船都需要特定数量的拖船,所以我们有这样的东西:

setClass("tugboat", slots=list(name = "character"))

tugboat1 <- new("tugboat", name = "tugboat 1")
tugboat2 <- new("tugboat", name = "tugboat 2")
tugboat3 <- new("tugboat", name = "tugboat 3")

setClass("ship", slots=list(name = "character", tugboats_required = "numeric"))

ship1 <- new("ship", name = "Titanic", tugboats_required = 1)
ship2 <- new("ship", name = "Black Pearl", tugboats_required = 1)
ship3 <- new("ship", name = "Interceptor", tugboats_required = 1)

我可以使用什么结构和逻辑来安装所有可能的场景,认为拖船不能同时参与两艘船,例如:

场景1:ship1 + tugboat1,ship2 + tugboat2,ship3 + tugboat3

场景2:ship1 + tugboat1,ship2 + tugboat3,ship3 + tugboat2

场景3:ship1 + tugboat2,ship2 + tugboat1,ship3 + tugboat3

场景4:ship1 + tugboat2,ship2 + tugboat3,ship3 + tugboat1

场景5:ship1 + tugboat3,ship2 + tugboat1,ship3 + tugboat2

场景6:ship1 + tugboat3,ship2 + tugboat1,ship3 + tugboat1

在上面的例子中,我认为每艘船只有一艘拖船(tugboats_required = 1),但如果这个数字是 2,例如该船的场景应该有 2 艘拖船。例子:

setClass("tugboat", slots=list(name = "character"))

tugboat1 <- new("tugboat", name = "tugboat 1")
tugboat2 <- new("tugboat", name = "tugboat 2")
tugboat3 <- new("tugboat", name = "tugboat 3")

setClass("ship", slots=list(name = "character", tugboats_required = "numeric"))

ship1 <- new("ship", name = "Titanic", tugboats_required = 1)
ship2 <- new("ship", name = "Black Pearl", tugboats_required = 2)

场景1:ship1 + tugboat1,ship2 + tugboat2 + tugboat3

场景2:ship1 + tugboat2,ship2 + tugboat1 + tugboat3

场景3:ship1 + tugboat3,ship2 + tugboat1 + tugboat2

问题不在于数学,问题在于在对象声明后组织数据。

标签: rdata-structures

解决方案


推荐阅读