How to create a rank correlation matrix in an elegant way in R, given a data frame with many columns? I could not find the built-in function, so I tried
> test=data.frame(x=c(1,2,3,4,5), y=c(5,4,3,2,1)) > cor(rank(test))
(only 2 columns for simplicity, real data have 5 columns), which gave
> Error in cor(rank(test)) : supply both 'x' and 'y' or a matrix-like 'x'
I realized that this is because rank takes one vector. So i tried
> cor(lapply(test,rank))
to get the rank applied to each column in the data frame, treating the data frame as a list that gave an error
> supply both 'x' and 'y' or a matrix-like 'x'
and I finally got something working with
> cor(data.frame(lapply(test,rank))) xy x 1 -1 y -1 1
However, this seems rather verbose and ugly. I think there should be a better way - if so, then what?
source share