r - 使用列表获取原子错误
问题描述
我在 R 中实现了一个 A-star 算法,并在尝试使用 sapply 函数时不断收到“包装期间的错误:$ 运算符对原子向量无效”。
这是我的结构:
frontier <- list()
starting_node = list(1, 1)
frontier <- append(frontier, list(node = starting_node, cost = 0, path = list()))
costs = sapply(frontier,function(item)item$cost)
这将返回错误。有谁知道是什么导致了这个问题?我觉得好像我正在使用列表,$ 操作应该没问题?
解决方案
您的列表边界的成本元素实际上是未定义 $ 运算符的原子类型数字:
library(tidyverse)
frontier <- list()
starting_node = list(1, 1)
frontier <- append(frontier, list(node = starting_node, cost = 0, path = list()))
frontier %>% sapply(class)
#> node cost path
#> "list" "numeric" "list"
由reprex 包于 2021-09-09 创建(v2.0.1)
sapply
使用 $ 将函数应用于每个元素,因此每个元素本身必须是一个列表。
推荐阅读
- kubernetes - 使用 helm 更新 Kubernetes 作业
- node.js - 内部服务器错误 - 通过 aws lambda/api gateway/s3 上传 base64 时
- sql - 在 SQL 中使用 SELECT 了解“在使用它的上下文中无效”错误
- python - 有没有办法解决导入 ternsorflow 的 keras 问题?
- r - 根据组的差异创建一个值为 1 的新列
- c# - 尝试测试 FIFO 互斥锁 - 如果我在一个循环中启动测试线程,它不起作用,但如果我间隔 1 毫秒启动它们,它确实有效
- frontend - 如何创建和显示自定义 Jekyll 元数据
- javascript - 节点js中lodash中_.filter方法的问题
- ruby-on-rails - Ruby on Rails API + VueJS + CKEditor 图片上传到 S3
- python - TensorRT 没有使用 float16 (或如何检查?)