r - 用 R 列表中的 0 替换 numeric(0)
问题描述
我有一个清单:
myList
$`0`
$`0`$item1
numeric(0)
$`0`$item2
[1] 350
$`0`$item3
numeric(0)
$`1`
$`1`$item1
numeric(0)
$`1`$item2
[1] 56
$`1`$item3
numeric(0)
我在这个列表中使用了一个 sapply 函数,但是得到一个错误:
参数的无效“类型”(列表)
我怎样才能将所有转换numeric(0)
为 0,就像其他项目一样?
解决方案
假设您只有 1 个深度列表,这是最短的解决方案
my_list[lengths(my_list) == 0] <- 0
对于 2 深度列表
my_list <- lapply(my_list, function(x)x[lengths(x) == 0] <- 0)
推荐阅读
- c# - 设置位置 StreamReader 不起作用读取仅附加文本文件
- reactjs - 使用 Webpack 1.13 (ReactJS) 意外导出令牌
- swift - 基于 MacOS 文档的应用程序打开文件对话框搜索框不起作用
- ios - SAPUI5 Cordova iOS 应用程序 Ajax 调用不起作用(无限挂起)
- r - 因为和直到参数导致R中的错误
- c - Cooja - C 中的预期声明说明符
- android - 如果我使用 Google 的日历 API,我可以访问此屏幕吗?
- python - Python 记录链接、模糊匹配和去重
- json - 如何获取 https://stocks.exchange/api2/ticker
- android - 无法使用 DownloadManager 下载文件