R numeric variable, non null, non na but empty - r

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

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."

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

Related

Length Zero in r

Greetings I am getting an error of
Error in if (nrow(pair) == 0) { :argument is of length zero
I have checked the other answers but do not seem to work on a variable like mine. Please check code below, please assist if you can.
pair<-NULL
if(exists("p.doa.ym")) pair <- rbind(pair, p.doa.ym[,1:2])
if(exists("p.doa.yd")) pair <- rbind(pair, p.doa.yd[,1:2])
if(nrow(pair) == 0) {
print("THERE ARE NO MATCHES FOR TODAY. STOP HERE")
quit()
}
Since you set pair=NULL and then it might happen that pair stays null if those two if statements are not true, you either need to check if pair is null first, or you could set pair to an empty data frame, or something else.
One option:
if (!is.null(pair)) {
if (nrow(pair)==0) {
# your code
}
}
Another option:
pair=data.frame()
# your code

R error: dims do not match the length of an object

I am currently trying to run some code (if you need to know the purpose to help me, ask me, but I'm trying to keep this question short). This is the code:
par<-c(a=.5,b=rep(1.3,4))
est<-rep(TRUE,length(par))
ncat<-5
Theta<-matrix(c(-6,-5.8,-5.6,-5.4,-5.2,-5,-4.8,-4.6,-4.4,-4.2,-4,-3.8,-3.6,-3.4,-3.2,-3,-2.8,-2.6,-2.4,-2.2,-2,-1.8,-1.6,-1.4,-1.2,-1,-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,1.8,2,2.2,2.4,2.6,2.8,3,3.2,3.4,3.6,3.8,4,4.2,4.4,4.6,4.8,5,5.2,5.4,5.6,5.8,6))
p.grm<-function(par,Theta,ncat){
a<-par[1]
b<-par[2:length(par)]
z<-matrix(0,nrow(Theta),ncat)
y<-matrix(0,nrow(Theta),ncat)
y[,1]<-1
for(i in 1:ncat-1){
y[,i+1]<-(exp(a*(Theta-b[i])))/(1+exp(a*(Theta-b[i])))
}
for(i in 1:ncat-1){
z[,i]<-y[,i]-y[,i+1]
}
z[,ncat]<-y[,ncat]
z
}
However, when I try to run the code:
p.grm(par=par,Theta=Theta,ncat=ncat)
I get the following error:
Error: dims [product 61] do not match the length of object [0]
Traceback tells me that the error is occurring in the first for loop in the line:
y[,i+1]<-(exp(a*(Theta-b[i])))/(1+exp(a*(Theta-b[i])))
Could someone point me to what I'm doing wrong? When I try to run this code step by step outside of the custom p.grm function, everything seems to work fine.
It is a common mistake. When you write the for loop and you want it from 1 to ncat -1 remember to write it as for (i in 1:(ncat-1)) instead of for(i in 1:ncat-1) they are completly different.
You may also add to the function something to return return(z). Here it is the corrected code:
par<-c(a=.5,b=rep(1.3,4))
est<-rep(TRUE,length(par))
ncat<-5
Theta<-matrix(c(-6,-5.8,-5.6,-5.4,-5.2,-5,-4.8,-4.6,-4.4,-4.2,-4,-3.8,-3.6,-3.4,-3.2,-3,-2.8,-2.6,-2.4,-2.2,-2,-1.8,-1.6,-1.4,-1.2,-1,-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,1.8,2,2.2,2.4,2.6,2.8,3,3.2,3.4,3.6,3.8,4,4.2,4.4,4.6,4.8,5,5.2,5.4,5.6,5.8,6))
p.grm<-function(par,Theta,ncat){
a<-par[1]
b<-par[2:length(par)]
z<-matrix(0,nrow(Theta),ncat)
y<-matrix(0,nrow(Theta),ncat)
y[,1]<-1
for(i in 1:(ncat-1)){
y[,i+1]<-(exp(a*(Theta-b[i])))/(1+exp(a*(Theta-b[i])))
}
for(i in 1:(ncat-1)){
z[,i]<-y[,i]-y[,i+1]
}
z[,ncat]<-y[,ncat]
return(z)
}
p.grm(par=par,Theta=Theta,ncat=ncat)

Need to Print Value of a Variable using Paste in R

I am trying to create a data frame of various error messages based on Data to be cross checked between two dataframes and storing the message in a vector in an iterative manner . I am using the following snippet for this purpose :
> for(j in 1:nrow(MySQL_Data)){ date_mysql=
> paste("MySQL_Data[",j,",1]") date_red= paste("RED_Data[",j,",1]")
> body= c() if(!date_mysql == date_red) {
> body<- append(body,paste("'There is data missing for date",date_mysql,"in",table2)) }else {
> NULL }}
My table2 variable prints as MYSQL_Data[2,1] instead of the actual value of the variable which is a date
Following is the Output :
"'There is data missing for date MySQL_Data[ 2 ,1] in Dream11_UserRegistration"
Can someone help me with the error that I am committing here..
Thanks in Advance !
Your use of paste in the definitions of data_mysql and data_red makes no sense. I’m assuming that what you actually want is this:
data_mysql = MySQL_Data[j, 1]
data_red = RED_Data[j, i]
Furthermore, you’re resetting body in every loop iteration so it will only ever hold a single element.

removeSource() with `[` subset fails on empty j argument

This is a follow-up to removeSource() returning error on internal package function.
In that question, it was pointed out that there may be a bug in removeSource() when the function uses [ subsetting. I want to focus on that issue, so I wrote a new question here.
When the j argument in [ is empty, removeSource() fails.
Here's an example.
foo <- function(x) { x[1, ] }
removeSource(foo)
# Error in recurse(part[[i]]) : argument "part" is missing, with no default
bar <- function(x) { x[1, seq_along(x)] }
removeSource(bar)
# function (x)
# {
# x[1, seq_along(x)]
# }
I'm hesitant to call it a bug, so I'll first ask if this was done intentionally? Also, suppose I submitted the foo() function in a package to CRAN. Would it pass the testing?
Updates:
Sept 1, 2014: Bug report filed https://bugs.r-project.org/bugzilla/show_bug.cgi?id=15957
Sept 21, 2014: This was indeed a bug and according to the confirmed bug report is "soon to be fixed in R-devel and R-patched."
Fixed in version R 3.1.2
I'd say it was a bug. No sign of it reported here though:
https://bugs.r-project.org/bugzilla3/buglist.cgi?quicksearch=removeSource
Interestingly you get a different error once you try and debug the function by making a local copy.
> dput(removeSource,file="rs.tmp.R")
> rs = dget("rs.tmp.R")
rs is now a copy of removeSource, but not in the environment of the utils package.
> foo = function(x){x[1,]}
> rs(foo)
Error in `attr<-`(`*tmp*`, "srcref", value = NULL) : '*tmp*' is missing
> removeSource(foo)
Error in recurse(part[[i]]) : argument "part" is missing, with no default
rs works fine on a function without missing subs:
> bar = function(x){x[1]}
> rs(bar)
function (x)
{
x[1]
}
If you want a really minimal failing example, you don't need any subscripts or commas:
> foo = function(x){x[]}
> removeSource(foo)
Error in recurse(part[[i]]) : argument "part" is missing, with no default
I doubt this will trigger any CRAN flags since missing dimensions in subscripts probably occur in 90% of the packages currently on there...
Suggest you report it on the bug tracker, or ask on R-devel mailing list.

Unable to resolve an Argument is of length zero error error

I get an Argument is of length zero error when I run the below code
The code is from this blog -http://giventhedata.blogspot.in/2012/08/r-and-web-for-beginners-part-iii.html.
library(XML)
url<- "http://news.bbc.co.uk/2/hi/uk_politics/8044207.stm"
first<-"Abbott, Ms Diane"
url.tab <- readHTMLTable(url)
for (i in 1:length(url.tab)){
if (as.character(url.tab[[i]][1,1]) == first ) {print(first)}
}
I know that the url.tab[[5]][1,1]) does contain the string "Abbott, Ms Diane", and when I run IF statement in isolation replacing the i with 5, it runs fine. Any help would be appreciated. I also tried declaring i<-1 upfront. DInt change anything.
Some of your tables are in fact NULL.
So you have to test for is.null before trying to subset the table:
for (i in 1:length(url.tab)){
this.tab <- url.tab[[i]]
if(!is.null(this.tab)) if(as.character(this.tab[1,1]) == first ) {print(first)}
}
[1] "Abbott, Ms Diane"

Resources