I've been dealing for a while with this problem but still haven't found the optimal solution. Hope you can help me.
I have a dataset with several columns, that I need to filter. As an example let's say that I have the following:
DB <- data.frame(A = c(rep("GeneA",6), rep("GeneB",6)), B = c("one", "one", "two", "two", "three", "three", "one", "one", "two", "two", "three", "three"), C = c(1,2,5,4,8,5,8,7,4,5,1,8))
What I would like to have is a filtered dataset for each value in column A with the groups in column B with highest mean.
In that case the desired output would be:
DB <- data.frame(A = c("GeneA","GeneA","GeneB","GeneB"), B = c("three", "three", "one", "one"), C = c(8,5,8,7))
Searching on the web only have found the case where you can filter the highest value of each group and I need each row of the group.
With:
result <- DB %>%
group_by(A,B) %>%
summarize(c = mean(C))
I just obtain the means. I have also tried with aggegate, etc. but no way. And i'm sure that there is a easy way, probably with data.table.