raggregategroup-summaries

R programming data frame - returning value based on position


I was wondering if there was a way to pull out a value based on position in a vector, so for example I have a data frame with two Vectors, I have them grouped from the raw by V1 and them by V2, much like a ORDER BY in SQL. My problem arises when I try get out the 3rd Min per V1 Group type.

Ordered data frame...

V1  V2
Ford    18
Ford    16
Ford    15
Ford    14
Ford    12
**Ford  5**
Ford    2
Ford    1
Nisan   10
Nisan   9
Nisan   8
Nisan   7
Nisan   6
**Nisan     5**
Nisan   4
Nisan   3
Toyota  20
Toyota  19
Toyota  15
Toyota  12
Toyota  11
**Toyota    10**
Toyota  6
Toyota  2

Result I want in new data frame, 3rd min value per variable...

V1 V2
Ford 5
Nisan 5
Toyota 10

Thanks in advance.


Solution

  • With base R you could do something like

    aggregate(V2 ~ V1, df[order(df$V2), ], `[`, 3L)
    #       V1 V2
    # 1   Ford  5
    # 2  Nisan  5
    # 3 Toyota 10
    

    Or (per @akruns comment) using ave

    df[with(df, ave(V2, V1, FUN = order)) == 3L,]