0

I'm trying to test for correlation between x and y of my data using Spearman Rank-Order Correlation in R but encountered the following error:

Error in cor.test.default(x = Female_ChLO, y = Female_TBL, method = "spearman") :
'x' and 'y' must have the same length

This data "Female_ChLO" had an outlier removed. When tested on the data before removing the outlier, I didn't encounter this error message.

The data does have a lot of NA but they are vital to the test and I'm trying to include na.rm=T but have no idea how to. Would love to hear suggestions but not too complicated please as I'm new to R.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • 2
    A correlation relies on paired observations. If you want to 'remove' data from one of the variables, replace it with `NA` instead of deleting it. Otherwise your 2 lengths are going to be unequal and you'll error out because of this line in `cor.test` - `if (length(x) != length(y)) stop("'x' and 'y' must have the same length")` – thelatemail Apr 16 '21 at 04:07
  • Do `Female_ChLO` and `Female_TBL` not have the same number of observations? The observations need to be paired for that test. The order in which values appear is assumed to be the order in which the values are paired. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Apr 16 '21 at 04:07
  • *"When tested on the data before removing the outlier, I didn't encounter this error message."* Then you removed one datum from one of the vectors and it became shorter? Yes, `cor()` is right. – Rui Barradas Apr 16 '21 at 06:09

0 Answers0