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
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.
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
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
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).
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 tois.nan
and you'll replaceNaN
s instead ofNA
sHi,
Thank you for your attention, while I use
is.nan
insteadis.na
, encounter the following error: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==
.Hi, Thank you for your answer. I use the above code and encounter the following error:
I think we have a bit of misunderstanding here. Is your object called
gset
orExpressionSet
? If it'sgset
, you should be usinggset[is.nan(gset)]<-0
.If that doesn't work, Friederike's exact code could work for you.
is.na()
matchesNA
as well asNaN
.If
gset
containsNaN
but notNA
, you can simply dogset[is.na(gset)] <- 0
. If your object contains bothNA
s andNaN
, (andgset
is adata.frame
like object), you can do something like:Please try both approaches above and let us know how that works out for you.
Hi, Thanks for your follow up.
gset
class isExpressionSet
.The above-mentioned codes are for
data.farame
object, notExpressionSet
. See an example of the above codes below.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 itsAssayData
slot. You should be able to use:or
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 usingdata.frame[is.na(data.frame)]<-0
, it shouldn't work, and if it does, there's a bigger problem with the code.