0

Given a data frame such as the following, how do I get a rank order (e.g. integer column ranking the value in order from descending as "1,2,3") column output for every single column without writing out ever single column?

df <- data.frame(
  col1 = rnorm(100),
  col2 = rnorm(100),
  col3 = rnorm(100),
  col4 = rnorm(100))

rank_col1 rank_col2 rank_col3 etc...

jaykay
  • 41
  • 1

1 Answers1

0

Is this what you want?

df <- cbind(df, as.data.frame(apply(df, 2, rank)))

ZT_Geo
  • 403
  • 1
  • 11
  • Awesome thank you! I originally tried to iterate over   arrange(V1) %>%   mutate(Rank = 1:nrow(.)) but it didn't work – jaykay Jan 06 '23 at 22:02