将数据集与R中的csv文件中的数据匹配 [英] matching dataset with data in csv file in R

查看:92
本文介绍了将数据集与R中的csv文件中的数据匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有数据

mydat=structure(list(id = 1:6, x2 = c(12L, 12L, 12L, 12L, 12L, 12L), 
    x3 = c(12L, 12L, 12L, 12L, 12L, 12L)), .Names = c("id", "x2", 
"x3"), class = "data.frame", row.names = c(NA, -6L))

我也有csv文件

test=read.csv(path,sep=";", dec",")

它具有这种结构

test=structure(list(id = 1:5, x2 = c(12L, 12L, 12L, 12L, 12L), x3 = c(12L, 
12L, 12L, 12L, 12L)), .Names = c("id", "x2", "x3"), class = "data.frame", row.names = c(NA, 
-5L))

我该如何匹配这两个数据集,从而使 mydat 已删除与 test 具有相似ID的
观测值?

How can i match these 2 datasets in such way that from mydat were removed observations which have similar id with test?

IE输出必须为

id  x2  x3
6   12  12

mydat中导致 id 1,2,3,4,5 code>与 test 数据集相似。

推荐答案

您可以使用dplyr中的 anti_join `

You can use anti_join from dplyr`

 dplyr::anti_join(mydat,test)
Joining, by = c("id", "x2", "x3")
  id x2 x3
1  6 12 12

在基数R中:您可以将数据折叠成字符串并进行比较:

In base R: you can collapse down the data into strings and compare them:

mydat[!do.call(paste,mydat)%in%do.call(paste,test),]
  id x2 x3
6  6 12 12

这篇关于将数据集与R中的csv文件中的数据匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆