Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 251
antimuon Avatar asked Dec 15 '22 14:12

antimuon


2 Answers

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,]
like image 73
David Arenburg Avatar answered Apr 01 '23 10:04

David Arenburg


Try

library(data.table)#v1.9.5+
setDT(df1)[order(V2), list(V2=V2[3L]), by = V1]

Or as @DavidArenburg mentioned in the comments

setDT(df1)[, .SD[frank(V2, ties.method = "dense") == 3L], by = V1]

Or

library(dplyr)
 df1 %>% 
     group_by(V1) %>%
     filter(rank(V2)==3)

Or

 df1 %>%
     group_by(V1) %>% 
     arrange(V2) %>%
     slice(3L)
like image 22
akrun Avatar answered Apr 01 '23 11:04

akrun