I have the following data frame:
dat <- data.frame(id = c("a", "b", "c", "d"),
x1 = c(1, 3, 5, 7),
x2 = c(4, 2, 6, 0),
x3 = c(2, 2, 5, 9))
I now want to calculate the ranking per row across my three x columns and want to store that result into my dat data frame.
So the result could be stored in two ways:
a) ideally, there will be 4 new columns with the respective ranks or
b) there will be a new nested column that I probably need to unnest somehow.
I tried the following which at least gives me a list column.
dat %>%
rowwise() %>%
mutate(my_ranks = list(rank(c_across(starts_with("x")))))
But when I try to unnest, it will give me the ranks but it does so by creating new rows (i.e. each original case now appears four times). Although I guess I could somehow reshape this result with pivot_wider, it feels wrong to follow that route.
Any better/easier idea? Thanks.