I have a dataframe and would like to calculate the correlation (with Spearman, data is categorical and ranked) but only for a subset of columns. I tried with all, but R’s cor() function only accepts numerical data (x must be numeric, says the error message), even if Spearman is used.
One brute approach is to delete the non-numerical columns from the dataframe. This is not as elegant, for speed I still don’t want to calculate correlations between all columns.
I hope there is a way to simply say “calculate correlations for columns x, y, z”. Column references could by number or by name. I suppose the flexible way to provide them would be through a vector.
Any suggestions are appreciated.
if you have a dataframe where some columns are numeric and some are other (character or factor) and you only want to do the correlations for the numeric columns, you could do the following:
but