r - 删除基本 R 中列表的第 i+1 个向量中的公共元素
问题描述
我想知道如果我可以找到并删除i+1
列表的 -th 向量中的公共元素(在 base 中R
)?
例如:
x = list(a = 1:5, b = 3:7, c = 6:9) # Initial list
common <- # your solution # ? find common values
for(i in 1:length(x)) {
# your solution: ?create `b` removing common elements with `a`
# your solution: ?create `c` removing common elements with `b`
}
解决方案
当我们比较相邻元素时,使用setdiff
withlist
和last
元素first
删除并更新'x'list
x[-1] <- Map(setdiff, x[-1], x[-length(x)])
推荐阅读
- python - 从 Python 中的目录内容列表中获取文件名
- laravel - 从控制器 laravel 获取最近 7 天的数据
- java - Firestore:如何设置 GOOGLE_APPLICATION_CREDENTIALS?
- node.js - ReferenceError:名称未定义
- python - 如何删除似乎没有名称的框架上的特定标签?
- python - 计算大量而不会耗尽内存
- python - 在目录中创建所有文件名及其文件扩展名的列表
- javascript - 如何将此 React 钩子组件转换为功能组件
- google-cloud-platform - 使用私有 IP 地址和多个私有服务连接启动 CloudSQL
- cassandra - 是否可以限制用户仅查看 Cassandra 中的一组特定列?