r - 如何找到数据框中的内容而不是R中的其他内容
问题描述
我有两个数据框:代码和供应。代码(如下所示)由四个字段组成:状态、代码类型、代码、代码标题供应有 12 列,但其中三个是状态、代码类型和代码
下面是一个例子
state codetype code codetitle
32 15 123456 Something
32 15 123455 Something Else
32 10 123455 Something Else
从那里,我使用以下代码连接项目
supply1<- supply%>%mutate(supply1= paste0(state,codetype,code))
codes1<- codes%>%mutate(codes1= paste0(state,codetype,code))
我的问题是如何找出供应1中的状态、代码类型、代码的组合,而不是代码1。我会使用 excel 和 match 函数来执行此操作,但是有 190 万行,这超出了 Excel 的容量。
已经查看了有关 antijoin 的文档。但是,由于没有 ID 等通用字段,因此有点困惑。
解决方案
tidyverse
library(dplyr)
anti_join(supply, codes, by = c("state", "codetype", "code"))
# state codetype code codetitle
# 1 34 15 123459 Something_4
碱基R
codes$code_rn <- seq_len(nrow(codes))
supply$supply_rn <- seq_len(nrow(supply))
temp <- merge(codes, supply, by = c("state", "codetype", "code"))
temp
# state codetype code codetitle.x code_rn codetitle.y supply_rn
# 1 32 15 123455 Something_Else 2 Something_3 2
# 2 32 15 123456 Something 1 Something_2 1
supply[ !supply$supply_rn %in% temp$supply_rn, ]
# state codetype code codetitle supply_rn
# 3 34 15 123459 Something_4 3
(和一些列清理)
替代碱基R
这实际上是您开始的内容:
supply_id <- with(supply, paste(state, codetype, code, sep = "|"))
supply_id
# [1] "32 15 123456" "32 15 123455" "34 15 123459"
codes_id <- with(codes, paste(state, codetype, code, sep = "|"))
codes_in
# [1] "32|15|123456" "32|15|123455" "32|10|123455"
supply[!supply_id %in% codes_id,]
# state codetype code codetitle supply_rn
# 3 34 15 123459 Something_4 3
数据
codes <- read.table(header = TRUE, text="
state codetype code codetitle
32 15 123456 Something
32 15 123455 Something_Else
32 10 123455 Something_Else")
supply <- read.table(header = TRUE, text="
state codetype code codetitle
32 15 123456 Something_2
32 15 123455 Something_3
34 15 123459 Something_4")
推荐阅读
- html - Chrome 或 iOS Safari 没有为 PWA 触发“beforeinstallprompt”
- swiftui - 如何在 SwiftUI 中更新单个子视图?
- ios - 上传游戏但没找到
- ios - 编译器抛出预期模式错误,同时尝试从元组中提取值
- node.js - 无法在 Mongoose 和 Express 中调用嵌套键(未定义)
- c++ - 二元运算符声明的“静态”关键字
- python-3.x - 在 jupyter 中安装 psi4 环境时出现问题
- android - 如何删除饼图中的底部空间?
- git - 詹金斯如何创建一些带有推送标签的容器,称为name_v0.01?
- machine-learning - 回到 XOR 问题的基础 - 从根本上混淆