I am pretty noob in R and this is driving me crazy. When I try to add a new row to this data frame it gives me this strange error and instead of adding a new name it adds . Does anyone know how to solve this? I really need help
MENU <- function()
{
P1=readline(prompt="Insert Name1:")
P2=readline(prompt="Inset Name2:")
TA=data.frame(names=c(P1,P2),games=c(0,0),points=c(100,100),stringsAsFactors= F)
write.table(TA,"C://Users//aleja//Documents//names.txt")
TA=read.table("C://Users//aleja//Documents//names.txt")
P1=readline(prompt="Insert Name1:")
new=list(names=P1,games=0,points=100)
TA=rbind(TA,new)
}
So this is what happens:
Insert Name1:ale
Inset Name2:ana
Insert Name1:jose
**Warning message:
In `[<-.factor`(`*tmp*`, ri, value = "jose") :
invalid factor level, NA generated**
Thanks for the help :)
To keep everything in your function the same, it would be fixed by:
MENU<-fucntion()
{
P1=readline(prompt="Insert Name1:")
P2=readline(prompt="Inset Name2:")
TA=data.frame(names=c(P1,P2),games=c(0,0),points=c(100,100),stringsAsFactors= F)
write.table(TA,"C://Users//aleja//Documents//names.txt")
TA=read.table("C://Users//aleja//Documents//names.txt")
P1=readline(prompt="Insert Name1:")
new=data.frame(names=P1,games=0,points=100)
TA=rbind(TA,new)
}
Hope this helps,
Related
I have some problems with the viterbiTraining function from the HMM package.
I tried using it on a pretty straightforward hmm and a vector of observations.
Here's the code:
Emisije<-rep("IntervalC",length(Cl1.res))
Emisije[IntervalA[,1]]<-"IntervalA"
Emisije[IntervalB[,1]]<-"IntervalB"
The Emisije vector looks like this:
head(Emisije)
[1] "IntervalA" "IntervalA" "IntervalA" "IntervalC" "IntervalB" "IntervalA"
startProbs<-c(0.6873065,0.3126935)
transProbs<-matrix(c(0.8, 0.7, 0.2,0.3),ncol=2)
emissionProbs<-matrix(rep(1/3,6),ncol=3)
stanji<-initHMM(c("NizkaVar", "VisokaVar"), c("IntervalA", "IntervalB",
"IntervalC"), startProbs, transProbs, emissionProbs)
After running this everything works, except for the viterbiTraining function, which gives the following result:
viterbiTraining(stanji,Emisije)
Error in if (d < delta) { : missing value where TRUE/FALSE needed
Even the similar function baumWelch, which takes the exact same parameters, works without errors, so I really don't understand what's wrong here.
Can anyone please explain to me what I am doing wrong? Thank you in advance.
I am trying to delete all values in a list that have the tag ".dsw". My list is a list of files using the function list.files. This is my code:
for (file in GRef) {
if (strsplit(file, "[.]")[[1]][3] == "dsw") {
#GRef=GRef[-file]
for(n in 1:length(GRef)){
if (GRef[n] == file){
GRef=GRef[-n]
}
}
}
}
Where GRef is the list of file names. I get the error listed above, but I dont understand why. I have looked at this post: Error .. missing value where TRUE/FALSE needed, but I dont think it is the same thing.
You shouldn't attempt to to modify a vector while you are looping over it. The problem is your are removing items you are then trying to extract later which is causing the missing values. It's better to identify all the items you want remove first, then remove them. For example
GRef <- c("a.file.dsw", "b.file.txt", "c.file.gif", "d.file.dsw")
exts <- sapply(strsplit(GRef, "[.]"), `[`, 3)
GRef <- GRef[exts!="dsw"]
I'm trying to make a confidence interval for practice and I keep getting an error referring to:
an 'invalid number of breaks' at hist.default(boot.dist).
I'm pretty sure the problem is somewhere here.
Any advice or help would be very much appreciated at this point.
b=1000
boot.dist = rep(0,b)
for (i in 1:b) {
boot.sample = sample(ACS$Income, replace = TRUE)
boot.dist[i] = mean(boot.sample)
}
hist(boot.dist)
The problem is that ACS$Income is array of NA.
Example, this code will reproduce error exactly like yours:
boot.dist[1:1000]<-NA
hist(boot.dist )
Error in hist.default(boot.dist) : invalid number of 'breaks'
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
I've looked at some answers that were already posted here and was not quite able to find one that would help with my particular situation. I'm new to R so bear with me.
rm(list = ls())
dat=read.csv('C:\\Users\\Casandra\\Downloads\\roaches.csv',as.is=T)
ndata=nrow(dat)
param=read.csv('C:\\Users\\Casandra\\Downloads\\roaches+posterior+after+burnin.csv',as.is=T)
param.med=apply(param,2,mean)
yhat=param.med[1]+param.med[2]*dat$x
png('pred distribution.png')
plot(dat$x,dat$y,xlim=c(0,1),ylim=c(0,1.5),xlab='Covariate x',col='grey',ylab='')
lines(dat$x,yhat,col='red')
nsim=nrow(param)
yhat1=yhat2=matrix(NA,ndata,3)
for (i in 1:ndata){
media=param[,1]+param[,2]*dat$x[i]
yhat1[i,]=quantile(media,c(0.025,0.5,0.975))
tmp=rnorm(nsim,mean=media,sd=sqrt(param[,3]))
yhat2[i,]=quantile(tmp,c(0.025,0.5,0.975))
}
lines(dat$x,yhat1[,1],col='orange',lty=2)
lines(dat$x,yhat1[,3],col='orange',lty=2)
lines(dat$x,yhat2[,1],col='grey',lty=2)
lines(dat$x,yhat2[,3],col='grey',lty=2)
dev.off()
Above is my code and this is the error I am receiving
Error in[.data.frame(param, , 3) : undefined columns selected
The dat data set has 2 columns and the param data set has two as well.
Can anyone point me in the right direction for fixing my code?
Data sets are below:
dat: https://drive.google.com/file/d/0B9MIgQ2O0SHnakRzZ2p2bDhIZ2c/edit?usp=sharing
param: https://drive.google.com/file/d/0B9MIgQ2O0SHnTTVMU0E2TTRDR2M/edit?usp=sharing