Find the highest value within factor levels

if I have the following data file:

value factorA factorB 1 ae 2 af 3 ag 1 bk 2 bl 3 bm 1 ce 2 cg 

how can I get for each factor the greatest value and record from the related factor B ie

 value factorA factorB 3 ag 3 bm 2 cg 

Is this possible without prior use

 blocks<-split(factorA, list(), drop=TRUE) 

and then sorting each $ a block, as this will be done many times, and the number of blocks will always change.

+6
source share
2 answers

Here is one option using the basic R functions:

 maxRows <- by(df, df$factorA, function(X) X[which.max(X$value),]) do.call("rbind", maxRows) # value factorA factorB # a 3 ag # b 3 bm # c 2 cg 
+12
source

With your data

 df<- structure(list(value = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L), factorA = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L), .Label = c("a", "b", "c"), class = "factor"), factorB = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 3L), .Label = c("e", "f", "g", "k", "l", "m"), class = "factor")), .Names = c("value", "factorA", "factorB"), class = "data.frame", row.names = c(NA, -8L)) 

Using the ddply in plyr

 > df2<-ddply(df,c('factorA'),function(x) x[which(x$value==max(x$value)),]) value factorA factorB 1 3 ag 2 3 bm 3 2 cg 

Or

 > rownames(df2) <- df2$factorA > df2 value factorA factorB a 3 ag b 3 bm c 2 cg 
+4
source

Source: https://habr.com/ru/post/912872/


All Articles