使用plyr-R进行分块后,从一列中获取具有最高值的行 [英] Get row with highest value from one column after chunking with plyr - R
问题描述
假设我有一个看起来像这样的数据框:
Suppose I have a dataframe that looks like this:
v1 v2 v3 v4 v5 v6
r1 1 2 2 4 5 9
r2 1 2 2 4 5 10
r3 1 2 2 4 5 7
r4 1 2 2 4 5 12
r5 2 2 2 4 5 9
r6 2 2 2 4 5 10
我想获取v6中具有最高值的行,而该行在v1中具有值1. 由于上一个答案,我知道如何获取v1 = 1的所有行并选择其中的第一行问题:
I would like to get the row with the highest value in v6 that has the value 1 in v1. I know how to get all rows where v1 = 1 and select the first row of that, thanks to this answer to a previous question:
ddply( df , .variables = "v1" , .fun = function(x) x[1,] )
如何更改功能,以便在v6中获得具有最高值的行?
How can I change the function so that I get the row with the highest value in v6?
推荐答案
从先前的结果中,我将使用[
使用logical
比较器在第一个条件下进行子集化,然后在第二个条件下进行第二个子集化因为正如@sgibb在注释中指出的那样,v6
的max
值可能不在v1 == 1
所在的行中.
From the previous results, I'd use [
to subset on your first condition using logical
comparators and then do a second subset on your second condition because as @sgibb points out in the comments, the max
value of v6
might not be in a row where v1 == 1
.
# Subset to those rows where v1 == 1
tmp <- df[ df$v1 == 1 , ]
# Then select those rows where the max value of v6 appears
tmp[ tmp$v6 == max( tmp$v6 ) , ]
如果您想第一次出现,请使用which.max()
If you want the first occurence, use which.max()
这篇关于使用plyr-R进行分块后,从一列中获取具有最高值的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!