Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R extract the n-th lowest value by values of one column, for all the levels of another column

Tags:

dataframe

r

I have this data frame:

> df
   c1 c2
1   1  b
2   2  a
3   3  a
4   4  a
5   3  a
6   2  b
7   6  a
8   4  b
9   8  b
10  7  a

So for i=3, the output should be the 3rd lowest values of "c1" for both levels of "c2"

   c1  c2
   3   a
   4   b
like image 724
Vahid Mirjalili Avatar asked Dec 15 '25 02:12

Vahid Mirjalili


1 Answers

Essentially a variation on a theme:

aggregate(c1 ~ c2, df, function(x) sort(x)[3])
#   c2 c1
# 1  a  3
# 2  b  4
like image 125
A5C1D2H2I1M1N2O1R2T1 Avatar answered Dec 16 '25 17:12

A5C1D2H2I1M1N2O1R2T1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!