r - Adding a True or False column for a dataframe if it matches a column of a different dataframe in R?
问题描述
I am seeing a few answers for this with Pandas in Python, but haven't found something in R. I am looking to add a 1 or 0 OR True or False boolean type column to a dataframe if that dataframe's column has matching values with another dataframe's column.
As an example, say I have a dataframe 'awesome':
Name | Value |
---|---|
A | 1 |
B | 23 |
C | 1 |
D | 15 |
E | 18 |
F | 19 |
And another dataframe 'neat'
Name | Sports |
---|---|
A | Soccer |
B | Bball |
G | Ball |
N | Ball |
Z | Volley |
F | Boxing |
I'd like to add a True or False column to the 'awesome' dataframe if the Name column in 'awesome' has a match with the 'neat' name column.
So the 'awesome' column would be: | Name | Value | has_match | | -------- | ------|------------| | A | 1 | True | | B | 23 | True | | C | 1 | False | | D | 15 | False | | E | 18 | False | | F | 19 | True |
Can someone tell me how I could do this in R?
Note: GitHub markdown table doesn't seem to be working for some reason, if anyone knows how to fix it into tables let me know... but have created a pic for easier reference:
解决方案
您可以使用%in%
:
awesome_df$has_match <- awesome_df$Name %in% unique(neat_df$Name)
推荐阅读
- email - 有没有办法用 Kotlin 发送电子邮件
- vue.js - Primevue Grid 和 Flex 布局无法正确呈现
- java - 使用 nfs-client 包上传/下载文件在 n 次迭代后不起作用
- angular - 开玩笑,TypeScript:缺少分号错误
- xaml - Xamarin 表单 - ListView - 数组 - 标题
- ansible - Ansible - 如何遍历多个主机并在找到正确答案时停止?
- c# - HttpClient 属性未通过 DI 传递
- html - 如何使用 NodeJS 服务器和 Angular Front 获取图像?
- java - 无效参数的 Spring Rest Controller 异常处理
- keras - 张量板图像未更新