wilcoxonPairedR error

I just wasted a whole day searching for a solution for my problem.

So briefly, I have 2 datasets with 2 samples each from a wilcox_test. Now, I wanted to test for effect size. For the first dataset I receive an effect size "r", for the other I receive a error message:

library(rcompanion)
wilcoxonPairedR(x = sport_1_4$value, g = sport_1_4$variable)

Fehler in wilcoxonZ(x = x[as.numeric(g) == 1], y = x[as.numeric(g) == 2],  : 
  'x' and 'y' must have the same length

The short answer is a function wants two arguments of equal length to compare. For a more informative answer See the FAQ: How to do a minimal reproducible example reprex for beginners.

1 Like

Hi @rokochan95,
Welcome to the RStudio Community Forum.

The error message says that when your data is split into the two groups (based on the values in column sport_1_4$variable) those groups are NOT the same size; for a valid test they must be equal (and each in the correct matching order). So, the problem is in your data frame. You can check the group sizes as follows:

length(sport_1_4$variable[as.numeric(sport_1_4$variable) == 1])
length(sport_1_4$variable[as.numeric(sport_1_4$variable) == 2])

HTH
1 Like

Hi @DavoWW,

actually they are the same size:

> table(sport_1_4$variable)

sport_t1 sport_t2 sport_t3 sport_t4 
      38        0        0       38 

but when I run your recommendation:

> length(sport_1_4$variable[as.numeric(sport_1_4$variable) == 1])
[1] 38
> length(sport_1_4$variable[as.numeric(sport_1_4$variable) == 2])
[1] 0

The last part is the problem, that's different from my first dataset sport_1_2.
The Question is why the don't have the same length, even though they have.

sport_1_2 and sport_1_4 really are they same except the different values for the variable sport_t4 in sport_1_4.

Ok, I solved it by copying the dataset into a new excel sheet, and importing it into Rstudio. Now it works... I didn't change anything at all. Weird.

1 Like

Hi @rokochan95,
The reason your earlier code was not working is that your grouping variable was a factor with 4 levels not 2 (despite 2 of the 4 having no observations), and only the first 2 are used by the function - hence the error. Your data frame needed to be modified to remove the 2 redundant levels. See
help(droplevels)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.