r - How to retrieve global index while using ddply? -



r - How to retrieve global index while using ddply? -

i trying find best deal in terms of price/carat diamonds dataset plyr package

so do

new = ddply(diamonds, c("cut", "color", "clarity"), transform, ecart= price/carat - mean(price/carat)) best = ddply(new, c("cut", "color", "clarity"), summarize, which(ecart == min(ecart))

but when get

head(best) cutting color clarity ..1 1 fair d i1 4 2 fair d si2 49 3 fair d si1 39 4 fair d vs2 9 5 fair d vs1 2

so index seem takend subgroups beingness made ddply. here first index, 4, correspond global index. if new[2,] not of type fair, d, vs1 instance.

any thought on how can retrieve global index position ?

how instance, add together id column elegantly ? there improve solution ?

if you're trying identify diamond lowest value of ecart each unique combination of cut, color , clarity, maybe meant this:

new <- ddply(diamonds, c("cut", "color", "clarity"), transform, ecart= price/carat - mean(price/carat)) best <- ddply(new, c("cut", "color", "clarity"), .fun = function(x){x[which.min(x$ecart),]})

which doesn't require messing indices outside of each piece of diamonds passed .fun.

edit

hadley points out in comments that

ddply(new, c("cut","color","clarity"), subset, ecart == min(ecart))

is more elegant. correctly pull out rows fit status in case of ties minimum.

r indexing plyr which

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -