WGCNA Error- 'x' must be numeric.

Hi everyone, I'm trying to use WGCNA but I keep getting this error:
Error in numbers2colors(traitData, signed = FALSE) :
'x' must be numeric. For a factor, please use as.numeric(x) in the call.

Here is my code:

library(WGCNA)
allowWGCNAThreads()
options(stringsasfactors = FALSE)
datCompds0=read.csv("IntSigSQRT2.csv")

dim(datCompds0)
names(datCompds0)

traitData = read.csv("PARAM.csv");
dim(traitData)
names(traitData)
library(flashClust)
sampleTree = flashClust(dist(datCompds0), method = "average");

sizeGrWindow(8,5)
par(cex = 0.6);
par(mar = c(0,4,2,0))
plot(sampleTree, main = "Sample clustering to detect outliers", sub="", xlab="", cex.lab = 1.5, cex.axis = 1.5, cex.main = 2)

abline(h = 20, col = "red");

sampleTree2 = flashClust(dist(datCompds0), method = "average")

traitColors = numbers2colors(traitData, signed = FALSE);

I've tried adding as.numeric(x) into the last line but to be honest, I'm not entirely sure how to do this properly! This is some output showing my data:

dim(datCompds0)
[1] 18 26
names(datCompds0)
[1] "Sample" "C01" "C02" "C03" "C04" "C05" "C06" "C07" "C08" "C09" "C10"
[12] "C11" "C12" "C13" "C14" "C15" "C16" "C17" "C18" "C19" "C20" "C21"
[23] "C22" "C23" "C24" "C25"
traitData = read.csv("PARAM.csv");
dim(traitData)
[1] 18 2
names(traitData)
[1] "Sample" "Treatment"

Any help would be greatly appreciated :slight_smile:

Try adding

Strings.as.Factors = FALSE

to the function call.

unfortunately I'm still getting the same error message

Can you share the results of str(traitData)?

These are the results:

str(traitData)
'data.frame': 18 obs. of 2 variables:
Sample : chr "BSB1R" "BSB2R" "BSB3R" "BSB4R" ... Treatment: chr "B" "B" "B" "B" ...

Without a reprex—see the FAQ: How to do a minimal reproducible example reprex for beginners, I'm having to guess, but next try

traitData$Sample <- as.factor(traitData$Sample)
traitData$Treatment <- as.factor(traitData$Treatment)

Converting to factors will change the internal representation to numeric. Should work.

Still the same error:
Error in numbers2colors(traitData, signed = FALSE) :
'x' must be numeric. For a factor, please use as.numeric(x) in the call.

Without the two csv files, it’s difficult to speculate further.

Almost there!

True, it changes the internal representation, but what is the answer to as.numeric() (which is what is used in the source of numbers2colors())?

The answer is literally in the error message :slight_smile:

So let's try it:

xx <- c("BSB1", "BSB2", "BSB1")

class(xx)
#> [1] "character"
is.numeric(xx)
#> [1] FALSE

xx_factor <- as.factor(xx)

class(xx_factor)
#> [1] "factor"
is.numeric(xx_factor)
#> [1] FALSE

xx_num <- as.numeric(xx_factor)

class(xx_num)
#> [1] "numeric"
is.numeric(xx_num)
#> [1] TRUE


WGCNA::numbers2colors(xx)
#> 
#> Error in WGCNA::numbers2colors(xx): 'x' must be numeric. For a factor, please use as.numeric(x) in the call.

WGCNA::numbers2colors(xx_factor)
#> Error in WGCNA::numbers2colors(xx_factor): 'x' must be numeric. For a factor, please use as.numeric(x) in the call.

WGCNA::numbers2colors(xx_num)
#>      [,1]     
#> [1,] "#FFFFFF"
#> [2,] "#FF3300"
#> [3,] "#FFFFFF"

Created on 2023-05-11 with reprex v2.0.2

Yay! So if you go that route, you need two steps: convert the characters to factor, so R will "tally up" the identical names and use an internal numeric representation; then force convert to numeric, to make that internal representation the external one.

Now, a separate question: are you sure you should do that? If I'm not mistaken, in the tutorial you're following, the "traits" are numeric by nature, for example "weight_g" will have numerical values from say 10 g to 50 g.

In your case the traits appear to be categorical, so I'm really not sure encoding them with a color scale is appropriate. I haven't used WGCNA a lot though, so I can't tell you what is the right way to go off the top of my head, I'm just suggesting you keep an eye out for the right way to compare your traits with WGCNA modules.

1 Like

This topic was automatically closed 42 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.