r - 尝试逐行比较两个数据帧与 R 中的许多列并标记不正确的行
问题描述
我实际上试图做的是将一个具有有限数量的行和列的数据集(其中每一行都被验证为正确)与另一个将随时间变化的数据集进行比较,我需要确定其中的哪些行第二个数据集与第一个不匹配。这可以与我有答案键(数据框 1)和一个包含数百万行的数据框进行比较,我需要将其与答案键进行比较以确定哪些行匹配。
我已经阅读了很多解决方案,但还没有找到一个简洁的解决方案 - 有什么建议吗?
添加示例数据集以支持 - 数据集 1
library(tibble)
df1 <- tribble(
~bc, ~var1, ~var2, ~var3,
"A", 324, 468, 462,
"B", 223, 362, 328,
"C", 187, 200, 229,
"D", 286, 455, 423)
数据集 2
df2 <- tribble(
~bc, ~var1, ~var2, ~var3,
"A", 324, 468, 462,
"B", 223, 362, 328,
"C", 187, 200, 229,
"D", 286, 455, 423,
"A", 324, 468, 462,
"B", 223, 362, 421,
"D", 286, 455, 423)
我要做的是通过与数据集 1 匹配的变量 bc 检查数据集 2。例如,数据集 2 中的第二个 D bc 不匹配不匹配数据集 1
解决方案
没有看到任何数据,很难回答你。
使用 which 函数可以告诉您哪些行符合某些条件。下面是一个如何使用 which 的示例。您可以将其更改为说 which(df2$answers %in% df1$answer_key) 或类似内容
# Load the data
data(iris)
# Take a look
head(iris)
which_example <- c(5.4, 4.6)
# The way I think of which is to ask R "which rows in iris$Sepal.Length are 5.4?"
which(iris$Sepal.Length %in% 5.4)
which(iris$Sepal.Length %in% which_example)
# Once you have the rows, you can display only those specific rows and all or some columns
# The format is df[row,column]
# Which gives the rows. You can leave column blank to get all or enter specific ones
iris[which(iris$Sepal.Length %in% 5.4),]
iris[which(iris$Sepal.Length %in% 5.4),c(2,4)]
推荐阅读
- pip - Statsd 和 Airflow 安装步骤
- c# - Android java.io.IOException即使在读取Mifare Classic时认证成功
- python - 使用 SocketIO 关闭 Change Stream Mongo db 的正确方法 - Flask
- tensorflow - SSD-Mobilenetv2 300x300 - Tensorflow 异议检测 API
- algorithm - 给定一个图 G 和一个顶点子集 A,编写一个有效的算法,如果它们存在,则打印两个集合 B,C 的 V 子集,这样:(继续)
- python - 类型错误:is_peak() 缺少 1 个必需的位置参数:'start_time'
- python - 从数据框创建对象
- paypal - 支付网关(Nordea)返回哈希无效
- javascript - 从自定义数据属性数组中过滤
- javascript - 使用 javascript 验证用户输入