This dataframe:
df <- data.frame(
stringsAsFactors = FALSE,
District = c("Dob", "Dab", "Dec", "Doi", "Dun", "Das"),
PBC = c(254L, 21L, 93L, 1601L, 136L, 169L),
PTC = c(65L, 147L, 27L, 1321L, 239L, 22L),
PCC = c(242L, 0L, 0L, 374L, 1L, 69L),
PKL = c(0L, 23L, 0L, 529L, 0L, 0L)
)
I want to obtain the second highest value in each row and assign it to a new column. I use:
MyFunc <- function(R,Pos){
tmp <- sort(R,decreasing = TRUE)
colnames(df)[which(R==tmp[Pos])]
}
df |> rowwise() |>
mutate(A = MyFunc(c_across(PBC:PKL),Pos = 2))
But I don't get the desired result. I get
District PBC PTC PCC PKL A
<chr> <int> <int> <int> <int> <chr>
1 Dob 254 65 242 0 PTC
2 Dab 21 147 0 23 PCC
3 Dec 93 27 0 0 PBC
4 Doi 1601 1321 374 529 PBC
5 Dun 136 239 1 0 District
6 Das 169 22 69 0 PTC
where A needs to be PCC in row 1, PKL on row 2, PTC on row 4, PCC in row 5.....