0

I keep getting the below error:

y <- as.factor(data$Pos.44)
x <- as.factor(data$Neg.44)
wilcox.test( x ~ y,  
                 alternative='two.sided', 
                 paired=FALSE, var.equal = TRUE, 
               conf.level = 0.95)

Error in wilcox.test.formula(x ~ y, alternative = "two.sided", paired = FALSE, : grouping factor must have exactly 2 levels

I have tried turning the data into factors, as numbers and have told r that the header is TRUE. Any ideas??

Here are some of the data.

Neg 44      Pos 44
13.8228455  7.126694925
13.8228455  8.402648457
13.8228455  8.402648457
6.85288518  12.27621627
9.232427575 
11.31157309 
195.5120218 
82.61317544 
9.751455813 
4.490663867 
7.732335943 
3.72520288  
17.51041865 
8.56912795  
17.59120857 
12.3571618  
Roman
  • 17,008
  • 3
  • 36
  • 49
  • Can you please share data in a reproducible, "copy&paste"-able format? Use `dput` or the code to generate sample data. Also use the editing tools to properly format code, data and text. – Maurits Evers Jul 18 '18 at 09:32
  • For what I can understand, you don't need to turn your numeric variables into factors, what you need is [Reshaping data.frame from wide to long format](https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format). – Rui Barradas Jul 18 '18 at 09:44

2 Answers2

0

Your format is wrong. The inputs to the Mann-Whitney test requires one column with values, and another column with a two level factor. You've tried to feed it two numeric vectors converted to factors. See the example below:

x=c(rnorm(50,0,1),rnorm(50,1,1))
y=rep(c('A','B'),c(50,50))
wilcox.test(x~y, alternative='two.sided',  paired=FALSE, var.equal = TRUE,  conf.level = 0.95) 
Rohit
  • 1,967
  • 1
  • 12
  • 15
  • Thanks. So the script would be? data <- read.csv("Relative.44.csv", header = T) x <- as.factor (data$Neg.44) y <- as.factor (data$Pos.44) x=c(rnorm(50,0,1),rnorm(50,1,1)) y=rep(c('A','B'),c(50,50)) wilcox.test(x~y, alternative='two.sided', paired=FALSE, var.equal = TRUE, conf.level = 0.95) – Alyce Belle Jul 18 '18 at 10:01
0

You can try

library(tidyverse)
d %>% 
  gather(k,v) %>% 
  with(.,wilcox.test(v ~ k, data=., exact =F))

Wilcoxon rank sum test with continuity correction

data:  v by k
W = 45, p-value = 0.2367
alternative hypothesis: true location shift is not equal to 0

Or visualize your data using a boxplot

library(ggbeeswarm)
library(ggsignif)
d %>% 
  gather(k,v) %>% 
  ggplot(aes(k,v)) + 
   geom_boxplot() +
   geom_beeswarm() + 
   ggsignif::geom_signif(comparisons = list(c("Neg44", "Pos44")))

enter image description here

Your data

d <- read.table(text="Neg44 Pos44
13.8228455  7.126694925
13.8228455  8.402648457
13.8228455  8.402648457
6.85288518  12.27621627
9.232427575 
11.31157309 
195.5120218 
82.61317544 
9.751455813 
4.490663867 
7.732335943 
3.72520288  
17.51041865 
8.56912795  
17.59120857 
12.3571618",header=T,  fill=T)
Roman
  • 17,008
  • 3
  • 36
  • 49