R code that iteratively creates a "rank_order" column for every column in a given dataframe

35 views Asked by At

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...

1

There are 1 answers

1
ZT_Geo On

Is this what you want?

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