删除重复,保持最大绝对值的输入

编程入门 行业动态 更新时间:2024-10-28 19:35:09
本文介绍了删除重复,保持最大绝对值的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 假设我有四个样本:id = 1,2,3和4,每个样本都有一个或多个测量值:

> a< - data.frame(id = c(1,1,2,2,3,4),value = c(1,2,3,-4,-5,6))> a id值 1 1 1 2 1 2 3 2 3 4 2 -4 5 3 -5 6 4 6

我想删除重复项,每个ID只保留一个条目 - 绝对值最大的条目的值列。也就是我想要的:

> a [c(2,4,5,6)]] id值 2 1 2 4 2 -4 5 3 -5 6 4 6

如何在R中执行此操作?

解决方案

aa< - a [order(a $ id, $ value)),] #sort由id和反向abs(值) aa [!duplicateated(aa $ id),]#取第一行每个id id值 2 1 2 4 2 -4 5 3 -5 6 4 6

Let's say I have four samples: id=1, 2, 3, and 4, with one or more measurements on each of those samples:

> a <- data.frame(id=c(1,1,2,2,3,4), value=c(1,2,3,-4,-5,6)) > a id value 1 1 1 2 1 2 3 2 3 4 2 -4 5 3 -5 6 4 6

I want to remove duplicates, keeping only one entry per ID - the one having the largest absolute value of the "value" column. I.e., this is what I want:

> a[c(2,4,5,6), ] id value 2 1 2 4 2 -4 5 3 -5 6 4 6

How might I do this in R?

解决方案

aa <- a[order(a$id, -abs(a$value) ), ] #sort by id and reverse of abs(value) aa[ !duplicated(aa$id), ] # take the first row within each id id value 2 1 2 4 2 -4 5 3 -5 6 4 6

更多推荐

删除重复,保持最大绝对值的输入

本文发布于:2023-11-29 18:06:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647190.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绝对值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!