This is a test version of Biostars. For the public version, visit https://www.biostars.org.
WGCNA R Dataframe using

Hej guys,

I want to use WGCNA to cluster genes. I have a dataframe like this:

Dataframe

I get this data because that is not a matrix afterall.

  datExpr must contain numeric data.

How can use this dataframe as a matrix by using R?

Thanks.

r bioconductor wgcna dataframe

Show us your code and tell us what you've tried. Googling the error message should be enough to solve this problem.

DatExpr=read.csv("filtered_reduced_rc.csv")
gsg = goodSamplesGenes(datExpr, verbose = 3)

Error in goodGenes(datExpr, weights, goodSamples, goodGenes, minFraction = minFraction,  : 
datExpr must contain numeric data.

What does str(head(datExpr[,1:5]) show? Also, you're reading into DatExpr and using datExpr in the next line. Is that a typo or are they 2 different objects?

> str(head(datExpr[,1:5]))
'data.frame':   6 obs. of  5 variables:
 $ Genes             : chr  "ENSG00000182199" "ENSG00000157870" "ENSG00000077092" "ENSG00000047936" ...
 $ F02283_L1_S49_L006: int  1517 3712 29 104 0 460
 $ F02284_L1_S50_L006: int  1932 2859 17 51 3 813
 $ F02320_L1_S14_L002: int  2246 2574 65 8 0 454
 $ F02357_L1_S50_L006: int  2860 1964 125 3 0 1321
> 

it is a typo.

From your screenshot, I thought the gene IDs were rownames but you have them as a non-numeric column, which makes the error kinda obvious, don't you think?

Use as.matrix on the dataframe after excluding the first column.

1 answer

as.matrix

This is count data; so I would strongly recommend filtering it for low counts and normalizing it with limma

expr <- matrix(as.integer(your.df), nrow=nrow(your.df))
rownames(expr) <- rownames(your.df)
colnames(expr) <- colnames(your.df)
med.cnt <- apply(expr, 1, median)
expr.vm <- limma::voom(expr)$E
expr.vm <- expr.vm[med.cnt > 5,]  # or so -- see how many genes there are and how stable your results are

expr.for.wgcna <- t(expr.vm)  # WGCNA expects genes to be columns

thank but i got this error.

> expr <- matrix(as.integer(DatExpr), nrow=nrow(DatExpr))
Error in matrix(as.integer(DatExpr), nrow = nrow(DatExpr)) : 

 'list' object cannot be coerced to type 'integer'

Log in to answer this question.