Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R numeric variable, non null, non na but empty

Tags:

null

r

numeric

na

Hi eveyrone ##.

I got some problem with R that I can't fix: Currently i'm working with GEOquery package and I want to retrieve some informations in metadata of gse files.

More precisely I'm looking for the channel label (for exemple Cye3). Here's a sample of my code :

>library(GEOquery)
>gse<-getGEO("GSE2253",GSEMatrix=TRUE,destdir=".")
>gse<-gse[[1]]
>gse$label_ch1[1]
 V2 

Levels:  According to Affymetrix protocol (biotin)`

And here's my problem

`> is.na(gse$label_ch1[1])
  V2 
 FALSE 
> is.null(gse$label_ch1[1])
[1] FALSE`

This GSE file is a text file and in the line corresponding to the label (!Sample_label_ch1) there is no value.So, here's what I'v done for my work:

`if(is.na(gse$label_ch1[1])){
color<-"Non specified"
} else {
label<-gse$label_ch1[1]
}`

So, if I got no informations for the channel I just say "non specified", else, I return the value. But I'v got error with this if/else statement in my script:

Error in if (file == "") file <- stdout() else if (is.character(file)) { : the length of argument is null

Sorry if the error traduction is not exact, my R version is in French ^^.

I tried

if(as.character(gse$label_ch1[1])=="")

But it doesn't work either

If someone has an idea to help me ^^

Thanks in advance!

Script:

sample<-NULL
output<-NULL
gse<-NULL
color<-NULL


series_matrix<-dir(getwd(),pattern="*series_matrix.txt")
series_matrix<-unlist(strsplit(series_matrix,"_")[1])
for(i in 1:length(series_matrix)){

gse<-getGEO(series_matrix[i],GSEMatrix=TRUE,destdir=".")
gse<-gse[[1]]

if(length(gse$label_ch1[1])==0){
 color<-"Non specified"
 } else {
 color<-gse$label_ch1[1]
 }
 print (color)


sample<-cbind(as.character(gse$title),as.character(gse$geo_accession))
outputsample<-paste(getwd(),"/sample.txt",sep="")
write.table(paste("txt",color,sep=""),output,
row.names=FALSE,col.names=FALSE,sep="\t",quote=FALSE)
write.table(sample,outputsample,
row.names=FALSE,col.names=FALSE,sep="\t",quote=FALSE,append=TRUE)


Feature_Num<-list(1:length(featureNames(gse)))
Gene_Symbol<-pData(featureData(gse)[,11])
Probe_Name<-pData(featureData(gse)[,1])
Control_Type<-pData(featureData(gse)[,3])
liste<-as.character(sampleNames(gse))
for(i in 1:lenght(liste)){
 values<-cbind(Feature_Num,Gene_Symbol,Probe_name,Control_Type,exprs(gse)[,i])
 colnames(values)<-c("Feature_Num","Gene_Symbol",
 "Probe_Name","Control_Type","gMedianSignal")
 write.table(values,paste(getwd(),"/Ech",liste[i],".txt",sep=""),
 row.names=FALSE,quote=FALSE,sep="\t")
 }
}

Don't hesitate if you want explication about lines in this script

like image 845
Maryvonne Avatar asked Feb 17 '26 10:02

Maryvonne


2 Answers

Yes, in R you can create a zero-length object:

 foo<-vector()
 foo
logical(0)

Then change it:  

foo<-NULL
foo
NULL

It's confusing at first, but if you ever took some abstract algebra, you may remember the difference between the "empty set" and a set whose only element is the "empty set."

like image 147
Carl Witthoft Avatar answered Feb 18 '26 23:02

Carl Witthoft


Asking on other forum I finally get a solution for this non NULL/non NA problem:

the gse$label_ch1[1] is numeric of length 1

> length(gse$label_ch1[1])
[1] 1

but we can transform this variable in character:

> as.character(gse$label_ch1[1])
[1] ""

and with this line

> nchar(as.character(gse$label_ch1[1]))
[1] 0

we can see that I can see if the gse$label_ch1[1] value is really empty or not

Thank you all for your help!

Cheers

like image 25
Maryvonne Avatar answered Feb 18 '26 22:02

Maryvonne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!