Mi error empieza al iniciar el primer loop, donde no me deja sustituir un valor de un data.frame por otro valor (factor). Encima sale que hay un error en una condición y no sé por qué. Cuando prueben el código, verán que sale:
Error in if (Playersmatrix[b, 1] == Emptyspace[1]) { :
valor ausente donde TRUE/FALSE es necesario
Además: Warning message:
In [<-.factor
(*tmp*
, iseq, value = 1L) :
invalid factor level, NA generated
Nunca había programado en mi vida. Muchas gracias.
numberSymbols<-function(symbols)
{
symbols=readline(prompt="How many consecutive symbols are required to win? ")
if((symbols=="1")|(symbols=="2")|(symbols=="3")|(symbols=="4")|(symbols=="5")|(symbols=="6")|(symbols=="7")|(symbols=="8")|(symbols=="9")|(symbols=="10")) cat (" ")
else
{
print("Number of symbols incorrect")
numberSymbols(symbols)
}
symbols=as.integer(symbols)
return(symbols)
}
Option1<-function(askData)
{
Playersmatrix1=matrix("_",10,1)
Playersmatrix2=matrix(0,10,2)
Playersmatrix=data.frame(Playersmatrix1,Playersmatrix2, stringsAsFactors=FALSE)
colnames(Playersmatrix)=c("Names","Games","Points")
cat("Who are going to play? ")
Player1=readline(prompt="Who is the first player? ")
Player2=readline(prompt="Who is the second player? ")
symbols=numberSymbols(symbols)
print(Playersmatrix)
b=1
repeat
{
if(Playersmatrix[b,1]=="_"){Playersmatrix[b,1]=Player1; b=nrow(Playersmatrix)}
else { if(Playersmatrix[b,1]==Player1)
{answer=readline(prompt="This player is already in the file. Would you like to change the name? ")
if((answer=="Yes")|(answer=="yes"))
{Player1=readline(prompt="Write a new name, please "); Player1=as.factor(Player1); Playersmatrix[b,1]=Player1; b=nrow(Playersmatrix)} else cat (" ")} else {cat (" ") }}
b=b+1
if(b==nrow(Playersmatrix)+1) break
}
b=1
repeat
{
if (Playersmatrix[b,1]=="_"){Playersmatrix[b,1]=Player2; b=nrow(Playersmatrix)}
else { if(Playersmatrix[b,1]==Player2)
{answer=readline(prompt="This player is already in the file. Would you like to change the name? ")
if((answer=="Yes")|(answer=="yes"))
{Player2=readline(prompt="Write a new name, please "); Player2=as.factor(Player2); Playersmatrix[b,1]=Player2; b=nrow(Playersmatrix)} else cat (" ")} else{ cat (" ") }}
b=b+1
if(b==nrow(Playersmatrix)+1) break
}
print(Playersmatrix)
return(list(symbols, Player1, Player2))
}
Option1()