Hi Friederike,
Thank you for your guidance and useful link that you introduced. I mean NaNs, not NAs, NaNs is different from NAs.
Hi everyone,
Sometimes in microarray analysis, some datasets contain NaN values, I need to remove NaNs from gset to ex matrix will be without NaNs too.
How to remove NaNs from ExpressionSet (gset)?
I would be very pleased if you could guide me.
We need to use Biobase::exprs() to access matrix data, then assign as usual, see this reproducible example:
Reproducible example from the manual - An Introduction to Bioconductor’s ExpressionSet Class
library(Biobase)
dataDirectory <- system.file("extdata", package="Biobase")
exprsFile <- file.path(dataDirectory, "exprsData.txt")
exprs <- as.matrix(read.table(exprsFile, header=TRUE, sep="\t",
row.names=1,
as.is=TRUE))
minimalSet <- ExpressionSet(assayData=exprs)
# check the data
exprs(minimalSet)[1:3, 1:3]
# A B C
# AFFX-MurIL2_at 192.7420 85.75330 176.7570
# AFFX-MurIL10_at 97.1370 126.19600 77.9216
# AFFX-MurIL4_at 45.8192 8.83135 33.0632
# introduce some NaN
exprs(minimalSet)[1:2, 1:2] <- NaN
# check the data
exprs(minimalSet)[1:3, 1:3]
# A B C
# AFFX-MurIL2_at NaN NaN 176.7570
# AFFX-MurIL10_at NaN NaN 77.9216
# AFFX-MurIL4_at 45.8192 8.83135 33.0632
# replace NaN with zero
exprs(minimalSet)[ is.nan(exprs(minimalSet)) ] <- 0
# check the data
exprs(minimalSet)[1:3, 1:3]
# A B C
# AFFX-MurIL2_at 0.0000 0.00000 176.7570
# AFFX-MurIL10_at 0.0000 0.00000 77.9216
# AFFX-MurIL4_at 45.8192 8.83135 33.0632
You should be able to subset the ExpressionSet like a base-R matrix, i.e.
ExpressionSet[is.na(ExpressionSet)] <- 0
If that doesn't work, try the same approach with exprs(ExpressionSet) or assayData(ExpressionSet).
See this website for more ways of replacing NA's.
Hi Friederike,
Thank you for your guidance and useful link that you introduced. I mean NaNs, not NAs, NaNs is different from NAs.
Change is.na in Friederike's command to is.nan and you'll replace NaNs instead of NAs
Hi,
Thank you for your attention, while I use is.nan instead is.na, encounter the following error:
> ExpressionSet[is.nan(ExpressionSet)] <- 0
Error in is.nan(ExpressionSet) :
default method not implemented for type 'closure'
Hi,
try this code maybe it will work for you
ExpressionSet[ExpressionSet == NaN] <- 0
Hi,
Comparing to NaN with == won't work unless you've redefined ==.
> NaN == NaN
[1] NA
> is.nan(NaN)
[1] TRUE
Hi, Thank you for your answer. I use the above code and encounter the following error:
> ExpressionSet[ExpressionSet == NaN] <- 0
Error in ExpressionSet == NaN :
comparison (1) is possible only for atomic and list types
I think we have a bit of misunderstanding here. Is your object called gset or ExpressionSet? If it's gset, you should be using gset[is.nan(gset)]<-0.
If that doesn't work, Friederike's exact code could work for you. is.na() matches NA as well as NaN.
If gset contains NaN but not NA, you can simply do gset[is.na(gset)] <- 0. If your object contains both NAs and NaN, (and gset is a data.frame like object), you can do something like:
gset.tmp <- gset #So we don't end up corrupting the original object
gset.tmp[do.call(cbind, lapply(gset.tmp, is.nan))] <- 0 # This should replace NaNs with 0s
Please try both approaches above and let us know how that works out for you.
Hi,
Thanks for your follow up.
gset class is ExpressionSet.
> class(gset)
[1] "ExpressionSet"
attr(,"package")
[1] "Biobase"
The above-mentioned codes are for data.farame object, not ExpressionSet.
See an example of the above codes below.
> gset[is.nan(gset)]<-0
Error in is.nan(gset) : default method not implemented for type 'S4'
> gset[is.na(gset)] <- 0
Error in gset[is.na(gset)] <- 0 : object of type 'S4' is not subsettable
In addition: Warning message:
In is.na(gset) : is.na() applied to non-(list or vector) of type 'S4'
> gset.tmp <- gset
> gset.tmp[do.call(cbind, lapply(gset.tmp, is.nan))] <- 0
Error in as.list.default(X) :
no method for coercing this S4 class to a vector
> ExpressionSet[ExpressionSet == NaN] <- 0
Error in ExpressionSet == NaN :
comparison (1) is possible only for atomic and list types
This needs for someone to read the manual and figure out what sort of class ExpressionSet is.
From a quick read, it looks like the numeric data in an ExpressionSet object is stored in its AssayData slot. You should be able to use:
dfES<-as(gset, 'data.frame');
dfES[do.call(cbind, lapply(dfES, is.nan))] <- 0
or
aD <- assayData(gset);
aD[do.call(cbind, lapply(aD, is.nan))] <- 0
P.S.: Please do not use ExpressionSet like you would a variable, it's not. It's the class of a variable. Using it as a variable is like using data.frame[is.na(data.frame)]<-0, it shouldn't work, and if it does, there's a bigger problem with the code.
Thanks for all the suggestions and valuable guides. I convert ExpressionSet into a Matrix, following replace NaNs with zero and convert the matrix into ExpressionSet again.
> ex <- exprs(gset)
> ex[is.nan(ex)] <- 0
> exprs(gset) <- ex
> dim(ex)
[1] 47323 86
> dim(gset)
Features Samples
47323 86
Thank you for posting the answer. Can you please up-vote and / or Accept the other answers that have assisted you?

Just to be precise, some advice on your wording: The ExpressionSet object (in your case named "gset") contains a matrix of values, which can be accessed via exprs() (as you show).
Log in to answer this question.