在列中的列中找到唯一值的值
我想找到一列的唯一值,但是取走指定向量中的值。在下面的示例数据中,我想从列all_areas
中查找唯一值,abreation1
and aint2
中的值。 即结果应该是“城镇”,“城市”,“村庄”
set.seed(1)
area_df = data.frame(all_areas = sample(rep(c("foo", "bar", "big", "small", "town", "city", "village"),5),20),
number = sample(1:100, 20))
area1 = c("foo", "bar")
area2 = c("big", "small")
I'd like to find the unique values of a column, but take away values that are in specified vectors. In the example data below I'd like to find the unique values from the column all_areas
minus the values in the vectors area1
and area2
.
i.e. the result should be "town", "city", "village"
set.seed(1)
area_df = data.frame(all_areas = sample(rep(c("foo", "bar", "big", "small", "town", "city", "village"),5),20),
number = sample(1:100, 20))
area1 = c("foo", "bar")
area2 = c("big", "small")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用函数
setDiff
来查找all_areas
和abreat 1
andaint 2
组合之间的设置差异:You could use the function
setdiff
to find the set difference betweenall_areas
andarea1
andarea2
combined:我们可以在%中使用
%来创建逻辑向量,负(
!
)到subset
'all_areas'的其他元素,然后返回 唯一行带有
unique
output的We may use
%in%
to create a logical vector, negate (!
) tosubset
the other elements from 'all_areas' and then return the unique rows withunique
-output
使用
dplyr
方法:With a
dplyr
approach: