简体   繁体   English

R有条件地提取数据?

[英]R extracting data conditionally?

I have a simulation dataset that explores a set of parameter space, and each set of parameter are run multiple times (iterations), it looks like so: 我有一个模拟数据集,它探索一组参数空间,每组参数都运行多次(迭代),它看起来像这样:

p1    p2    p3  iteration  result
=================================
v3    v2    v1      1       23.8
v2    v1    v3      2       20.36
v3    v2    v1      2       28.8
v2    v1    v3      1       29.36
...

As can be seen from this example, both (v3, v2, v1) and (v2, v1, v3) are run twice. 从该示例可以看出,(v3,v2,v1)和(v2,v1,v3)都运行两次。 I am trying to extract only the rows with max result for each parameter setting, in this example: only row 3 and 4 should be kept, as they represent the best results from that parameter set. 我试图仅为每个参数设置提取具有最大结果的行,在此示例中:仅保留第3行和第4行,因为它们代表该参数集的最佳结果。 Is there a easy way to accomplish that in R? 有没有一种简单的方法可以在R中实现这一目标? Thanks 谢谢

df <- read.table(textConnection("p1    p2    p3  iteration  result
v3    v2    v1      1       23.8
v2    v1    v3      2       20.36
v3    v2    v1      2       28.8
v2    v1    v3      1       29.36"), header = T)

library(plyr)
ddply(df, .(p1,p2,p3), function(x) return(x[(which(x$result == max(x$result))), ]))

p1 p2 p3 iteration result
1 v2 v1 v3         1  29.36
2 v3 v2 v1         2  28.80

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM