R - aggregate data for 1 column by another column, based on statistices on a 3rd column -


Let me assume that I have an R data frame with 3 columns A , B

and C , where A values ​​are not all separate.

How do I get all the values ​​of A, the value of C for which B is minimum (for that value of A)? In some pseudo SQL code this happens: SELECT C WHERE B = MIN (B) GROUPBY A ?

I've looked at the total () function, I'm not sure it can complete it.

Total (B ~ A, Data = MyDataFrame, Minute) Only gives me the minimum score of B for each A, but then I do not know how to get related C value for.

In order to achieve the values, this is to minimize the data frame with the result of this aggregation, and / or it can only be done in gross () ?

Input:

  ABC 1 0 1 1 2 2 1 1 3 1 1 4 2 1 1 2 2 2 3 3 3 3 4  

Output:

1

1a The value of the value is A = 1

3 for C (0) A value of C is equal to 2

data.table package:

  Library (data-worthy) DT from the  Lt; - as.data.table (mydataframe) dt [, c [which.min (b)], by = "a"] # a v1 # 1: 1 1 # 2: 2 3  

or dplyr :

  library (dplyr) mydataframe%.% Group_by (a)%.% Summarize (res = c [that.min (b) ]) # A res # 1 2 3 # 2 1 1  

or by the original function by :

  (mydataframe, Mydataframe $ a, function (x) x $ c [that.min (x) $ b)]) #mydataframe $ a: 1 # [1] 1 # --------------- ------------------ -------------------------------- -------------- # mydataframe $ A: 2 # [1] 3  

Comments