比较R中的字符向量以查找唯一和/或缺失值 [英] Comparing character vectors in R to find unique and/or missing values

查看:94
本文介绍了比较R中的字符向量以查找唯一和/或缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字符向量,x和y。

I have two character vectors, x and y.

x <- c("a", "b", "c", "d", "e", "f", "g")
y <- c("a",      "c", "d", "e",      "g")

x中的值永远不会重复(即,它们都是唯一的)。向量y也是如此。我的问题是,如何让R比较两个向量,然后告诉我y相对于x缺少哪些元素?否则,我要R告诉我y缺少 b和 f。

The values inside x do not ever repeat (i.e., they are all unique). The same goes for vector y. My question is, how can I get R to compare the two vectors, and then tell me which elements are missing from y with respect to x? Otherwise stated, I want R to tell me that "b" and "f" are missing from y.

(请注意,在我的真实数据中,x和y各自包含数千个观测值,这就是为什么我要以编程方式执行此操作。可能有一个非常简单的答案,但我不确定要在R帮助文件中搜索什么。

(Note, in my real data, x and y each contain a few thousand observations, which is why I would like to do this programmatically. There is likely a very simple answer, but I wasn't sure what to search for in the R help files).

感谢任何可以提供帮助的人!

Thanks to anyone who can help!

推荐答案

setdiff(x,y)

将为您完成这项工作。

这篇关于比较R中的字符向量以查找唯一和/或缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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