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)
I am trying to use the command:
audio_play_sound()
I am trying to insert it into this piece of code, so that when the player jumps, a sound plays.
if (key_jump) && (jumps > 0)
{
jumps -=1;
vsp = -jumpspeed;
}
Code that causes problem:
if (key_jump) && (jumps > 0)
{
jumps -=1;
vsp = -jumpspeed;
audio_play_sound(snd_jump)
}
Simply inserting the line into the if statement does not work, and gives the error WRONG NUMBER OF ARGUMENTS IN FUNCTION. This is rather confusing, perhaps I am using the wrong command? Thanks in advance
The problem is stated in the error, you're providing the wrong number of arguments to the audo_play_sound function.
from the docs
audio_play_sound(index, priority, loop);
As the person above states your answer is audio_play_sound(snd_jump, 1, false).
I have tried to write a simple code to compute the median but I got an error.
This is what I wrote
median<-function(x){odd.even<-length(x)%%2 if (odd.even = = 0)(sort(x)[length(x)/2]+sort(x)[1+length(x)/2])/2 else (sort(x)[ceiling(length(x)/2)])}
and this is the error I got
Error: unexpected 'if' in "median<-function(x){odd.even<-length(x)%%2 if"
Thanks
Try this (you forgot the brackets {)
median<-function(x){
odd.even<-length(x)%%2
if (odd.even == 0){
(sort(x)[length(x)/2]+sort(x)[1+length(x)/2])/2
} else {
(sort(x)[ceiling(length(x)/2)])
}
}
As pointed out if you want not to use bracket you can always do this, with a new line on the if statement :
median<-function(x){
odd.even<-length(x)%%2
if (odd.even == 0) (sort(x)[length(x)/2]+sort(x)[1+length(x)/2])/2 else (sort(x)[ceiling(length(x)/2)])
}
Also a return(x) at the end, might help the reading process, although it is not compulsory.
This question already has answers here:
if/else constructs inside and outside functions
(2 answers)
Closed 10 years ago.
What is wrong with this if-else in my R program?
if(is.na(result)[1])
print("NA")
else
coef(result)[[1]]
I'm getting an error:
> if(is.na(result)[1])
+ print("NA")
> else
Error: unexpected 'else' in "else"
> coef(result)[[1]]
So then I added curly braces around the if and the else and now I get this error:
> if(is.na(result)[1]) {
+ print("NA")
Error: unexpected input in:
"if(is.na(result)[1]) {
¬"
> } else {
Error: unexpected '}' in "}"
> coef(result)[[1]]
Error: unexpected input in "¬"
> }
Error: unexpected '}' in "}"
It is that you are lacking curly braces. Try
if(is.na(result)[1]) {
print("NA")
} else {
coef(result)[[1]]
}
This matters less when your source an entire file at once but for line-by-line parsing (eg when you enter at the prompt) you have to tell R that more code is coming.
I think your problem is signaled with this error message:
"if(is.na(result)[1]) {
¬"
Notice that strange little symbol? You have gotten a non-printing character that looks like one of those old IBM end-of-line markers. It could be problem with your LOCALE strings or with your keyboard mapping or with code you got off the Internet. Hard to tell, but you should definitely try to get rid of them by backspacing over them/
Without curly brackets, the if ... else ... construct should be on one line only:
if(is.na(result)[1]) print("NA") else coef(result)[[1]]
If you only have one condition and two results it's syntactically easier to use ifelse()
ifelse(is.na(result)[1],
print("NA"),
coef(result)[[1]]
)
This runs for me without errors
x <- 1:5
result <- lm(c(1:3,7,6) ~ x)
if(is.na(result)[1]) {
print("NA")
} else {
coef(result)[[1]]
}
and produces
[1] -0.7
I am getting the following error. I can not figure out what is missing, as I seem to have all my brackets matched up.
Error: unexpected ')' in:
"{
if (grepl(propertiesData[x,'city'],population[z,'NAME'],ignore.case=TRUE) & (propertiesData[x,'stateLong']==population[z,'STATENAME')"
Here is the code of the loop:
for (z in c(1:nrow(population)))
{
if (grepl(propertiesData[x,'city'],population[z,'NAME'],ignore.case=TRUE) & (propertiesData[x,'stateLong']==population[z,'STATENAME'))
{
propertiesData[x,'population']=population[z,'POP_2009']
break
}
}
==population[z,'STATENAME'))
Seems like you forgot the closing bracket. Add it in and see what happens:
==population[z,'STATENAME']))
You're missing one ] at the end of line.
...==population[z,'STATENAME'] ))
You are missing "]" at the end : (propertiesData[x,'stateLong']==population[z,'STATENAME']))