如何使用几乎相同的列值将行放入R中?
我有一个带有名称列的数据集。如果存在一个较高的值,我想用较小的“ P”值掉下行。例如,在下面的数据集中,我想放下“ Dexas P5”和“ North Dakota P9”,因此我想删除ROW ID的3和5。最好的方法是什么?提前致谢!
ID | 名称 | 得分 |
---|---|---|
1 | 明尼苏达州P2 | 342 |
2 | Vermont P7 | 342 |
3 | Texas P4 | 65 |
4 | New Mexico | 643 |
5 | North Dakota P8 | 78 |
6 | North Dakota P9 | 245 |
7 | Texas P5 | 856 |
8 | Minnesota LP | 342 |
I have a dataset with a column of names. I would like to drop rows with the lesser "P" value, if there exists one with a higher value. For example, in the dataset below, I would like to drop the row ID's 3 and 5 since there exists a 'Texas P5' and a 'North Dakota P9.' What is the best way to do this? Thanks in advance!
ID | Name | Score |
---|---|---|
1 | Minnesota P2 | 342 |
2 | Vermont P7 | 342 |
3 | Texas P4 | 65 |
4 | New Mexico | 643 |
5 | North Dakota P8 | 78 |
6 | North Dakota P9 | 245 |
7 | Texas P5 | 856 |
8 | Minnesota LP | 342 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一种基本r方式。使用
AVE
通过name
将数据拆分,排除数字,并检查哪个组元素等于其最大元素。AVE
在这种情况下,返回与输入同一类的向量。因此,迫使逻辑并将原始数据框架归为逻辑。由
Here is a base R way. Use
ave
to split the data byName
excluding the numbers, and check which group element is equal to its greatest element.ave
returns a vector of the same class as its input, in this case character. So coerce to logical and subset the original data frame.Created on 2022-07-06 by the reprex package (v2.0.1)
另一个基本选项:
Another base option:
尝试此解决方案,以
name
name
值差异始终为2个字符的部分,前提是长2个字符,然后位于字符串的末尾:数据 :data:data :
Try this solution, which presupposes that the
Name
parts by which theName
values differ are always 2 characters long, preceded by whitespace, and positioned at the end of the string:Data:
使用
data.table
方法:Using a
data.table
approach: