r - 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
问题不在于数学,问题在于在对象声明后组织数据。
解决方案
推荐阅读
- java - 使用对象包装器在springboot中请求正文多个参数不起作用
- regex - 如何执行多个 grep 模式以在 grepped 字符串中查找值
- java - 如何在 Java CardLayout 中选择特定的卡片?
- flutter - 我正在使用 Getx 进行状态管理,当我将数据添加到服务器时,它不会在列表视图中出现较新的数据,直到热重启而列表视图位于 Obx 中
- python-3.x - 如果HDFS中不存在目录,如何使用pyspark动态创建目录并设置文件和目录权限
- keil - 为什么我不能在 Keil 中为 STM32F446 定义可执行变量?
- amazon-s3 - 如何将 S3 功能(组件)安装到 isilon(OneFS S3)?
- powerpivot - 过期和过期前消耗
- express - 使用特定参数获取带有猫鼬的图片
- linux - 如何从核心转储文件中提取 AT_EXECFN