本文介绍了删除重复,保持最大绝对值的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有四个样本: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 6I 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 6How 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
更多推荐
删除重复,保持最大绝对值的输入
发布评论