Given a data.frame like the one below, how do I find the kth largest value for each row from a range of columns:
Input:
| ID | i1 | i2 | i3 |
|---|---|---|---|
| 1 | NA | NA | NA |
| 1 | 0 | NA | NA |
| 1 | 1 | 0 | NA |
| 1 | 1 | 1 | NA |
| 1 | 0 | 1 | NA |
| 1 | 0 | 1 | 2 |
Desired Output:
| ID | i1 | i2 | i3 | k=1 | k=2 | k=3 |
|---|---|---|---|---|---|---|
| 1 | NA | NA | NA | NA | NA | NA |
| 1 | 0 | NA | NA | 0 | NA | NA |
| 1 | 1 | 0 | NA | 0 | 1 | NA |
| 1 | 1 | 1 | NA | 1 | 1 | NA |
| 1 | 0 | 1 | NA | 0 | 1 | NA |
| 1 | 2 | 1 | 0 | 0 | 1 | 2 |
I would like a solution that works with three columns like above (i1,i2,i3) but can also work for a dataframe with say 6 columns named (i1,i2,i3,i4,i5,i6) and more potentially. Assume these columns are always adjacent to each other and named like how I have them