R guessing game syntax - r

I'm having trouble with proper bracket syntax when making a guessing game. Here is a brief example of my code
number_result <- readline(prompt = "Choose a number btwn 1 & 100: ")
input <- 0
rand <- sample(seq(1,100), size = 1)
input = number_result
while(input != rand){
if(input < rand){
print("Higher!")
}
else if(input > rand){
print("Lower!")
}
else(input = rand){
return(print("You got it!"))
}
}
My error is:
Error: unexpected '{' in:
" }
else(input = rand){"
> return(print("You got it!"))
[1] "You got it!"
Error: no function to return from, jumping to top level
> }
Error: unexpected '}' in "}"
> }
Error: unexpected '}' in " }"
>

No return needed because you didn't define a function. You must also specify a stopping condition! Otherwise your while will run forever.
try this
number_result <- readline(prompt = "Choose a number btwn 1 & 100: ")
input <- 0
rand <- sample(seq(1,100), size = 1)
input = number_result
while(input != rand){
if(input < rand){
print("Higher!")
} else if(input > rand){
print("Lower!")
} else {
print("You got it!")
}
input <- input + 1
}

Related

Error: unexpected '}' in " }" and I don't know what to do

C <- rep(c('F','M'), each = 3, times = 4)
Gender <- NULL
for (i in C) {
if i == 'F'{
append(Gender,values = 'Female')
}
else {
append(Gender, values = 'Male')
}
}
and it shows Error: unexpected '}' in " }"
what should I fix in this code?
The error comes from if i == 'F'
C <- rep(c('F','M'), each = 3, times = 4)
Gender <- NULL
for (i in C) {
if (i == 'F'){ # The condition has to be wrapped by ()
Gender <- append(Gender,values = 'Female') # Reassign to Gender
} else { # It's better to put else this way
Gender <- append(Gender, values = 'Male')
}
}

Why can't I use in with an if condition

I have the following code:
x=rnorm(100,0,1)
x
a=0
for(i in x){
if(i in -1:1){
a<-a+1
}
}
I'm getting the following error:
Geeting error unexpected '}' in " }"
What am I doing wrong?
I changed the condition in the if statement. Is this what you want?
x=rnorm(100,0,1)
x
a = 0
for(i in x){
if(i > -1 & i < 1){
a <- a + 1
}
}

Getting "unexpected numeric constant" in code

I am trying to run my code but get the following error:
Error: unexpected numeric constant in: "
if((final.string[1+loop.check]) !=
(final.string[string.length-loop.check])){
return FALSE"
My code is as below:
inputString = "aabb"
string.length <- nchar(inputString)
compare.list <- strsplit(inputString,"")
final.string <- unlist(compare.list)
loop.check <- 0L
if(string.length %% 2 == 0){
loop.stop <- string.length/2
}
if(string.length %% 2 == 1){
loop.stop <- (string.length -1) / 2
}
while (loop.check<=loop.stop){
if((final.string[1+loop.check]) != (final.string[string.length-loop.check])){
return FALSE
break
}
else{
loop.check <- loop.check + 1
}
}
if(loop.check-1==loop.stop){
return TRUE
}
If I run just the (final.string[1+loop.check]) != (final.string[string.length-loop.check]) portion then the console returns FALSE so the code for the if statement condition seems to be working. However, when attempting to run the whole script I get the above error.

unexpected numeric constant graph

The requirement is using R base function to build up a function like this:
Function - is_isomorphic
Input - g1, a graph object; g2, a graph object.
Output - true if g1 and g2 are isomorphic, false if not.
Description - Check if the graph objects are isomorphic, meaning all vertices, edges, and weights are identical. Comparison of vertices should be based on names not indexes, indexes should only be used if vertex labels are not defined.
My code is as follows:
isomorphic <- function(g1,g2){
##first calculate number of vertices, if not equal, return false
if(length(g1) != length(g2)){
return FALSE
##then compare each vertix and its related edges and weights
} else {
for(i in 1:length(g1)){
for(j in 1:length(g2)){
if(names(g1[i]) == names(g2[j]){
flag = 1
if(g1[[i]]$edges != g2[[j]]$edges){
return FALSE
} else {
if(g1[[i]]$weights != g2[[j]]$weights){
return FALSE
} else {
return TRUE
}
}
}
}
if(flag != 1){
return FALSE
}
}
}
}
wrong message:
isomorphic <- function(g1,g2){
+ ##first calculate number of vertices, if not equal, return false
+ if(length(g1) != length(g2)){
+ return FALSE
Error: unexpected numeric constant in:
" if(length(g1) != length(g2)){
return FALSE"
> ##then compare each vertix and its related edges and weights
> } else {
Error: unexpected '}' in " }"
> for(i in 1:length(g1)){
+ for(j in 1:length(g2)){
+ if(names(g1[i]) == names(g2[j]){
Error: unexpected '{' in:
" for(j in 1:length(g2)){
if(names(g1[i]) == names(g2[j]){"
> flag = 1
> if(g1[[i]]$edges != g2[[j]]$edges){
+ return FALSE
Error: unexpected numeric constant in:
" if(g1[[i]]$edges != g2[[j]]$edges){
return FALSE"
> } else {
Error: unexpected '}' in " }"
> if(g1[[i]]$weights != g2[[j]]$weights){
+ return FALSE
Error: unexpected numeric constant in:
" if(g1[[i]]$weights != g2[[j]]$weights){
return FALSE"
> } else {
Error: unexpected '}' in " }"
> return TRUE
Error: unexpected numeric constant in " return TRUE"
> }
Error: unexpected '}' in " }"
> }
Error: unexpected '}' in " }"
> }
Error: unexpected '}' in " }"
> }
Error: unexpected '}' in " }"
>
> if(flag != 1){
+ return FALSE
Error: unexpected numeric constant in:
" if(flag != 1){
return FALSE"
> }
Error: unexpected '}' in " }"
> }
Error: unexpected '}' in " }"
> }
Error: unexpected '}' in " }"
> }
Error: unexpected '}' in "}"
Go aback and read the Intro to R to understand the syntax. Part of the problem is that 'return' is a function call and you have to have the value in parends.
Also get a text editor that highlights parends and bracket since you were missing some. Here is a function that is valid:
> isomorphic <- function(g1,g2){
+ ##first calculate number of vertices, if not equal, return false
+ if(length(g1) != length(g2)){
+ return( FALSE)
+ ##then compare each vertix and its related edges and weights
+ } else {
+ for(i in 1:length(g1)){
+ for(j in 1:length(g2)){
+ if(names(g1[i]) == names(g2[j])){
+ flag = 1
+ if(g1[[i]]$edges != g2[[j]]$edges){
+ return( FALSE )
+ } else {
+ if(g1[[i]]$weights != g2[[j]]$weights){
+ return (FALSE)
+ } else {
+ return( TRUE)
+ }
+ }
+ }
+ }
+
+ if(flag != 1){
+ return( FALSE)
+ }
+ }
+ }
+ }
>

for loop with if else statement in R

I know lots of people have posted about this, and I did look through the answers to write my code, but it's still not working... Can someone point out where i'm doing wrong please? many thanks in advance!
for(j in 1:1000){
for(i in 1:52){
if (i == 1){
r.sims[i,] <- r.sims[1]
}
else if (i == 2){
r.sims[i,] <- r.sims[2]
}
else (i > 2){
r.sims[i,] <- r.sims[i-1,]*ar1 + r.sims[i-2,]*ar2 + e.sims[i,] + e.sims[i-1,]*ma1
}
}
}
i have the following errors
Error: unexpected '{' in:
" }
else (i > 2){"
> r.sims[i,] <- r.sims[i-1,]*ar1 + r.sims[i-2,]*ar2 + e.sims[i,] + e.sims[i-1,]*ma1
Error in r.sims[i, ] <- r.sims[i - 1, ] * ar1 + r.sims[i - 2, ] * ar2 + :
replacement has length zero
> }
Error: unexpected '}' in " }"
>
> }
Error: unexpected '}' in " }"
>
> }
Error: unexpected '}' in " }"
>
Without understanding what your code is supposed to do, I have nevertheless made an example of how you might fix your script. I think the key is that you need to supply curly brackets {...} following your else statement in order for it to consider the following code.
Example:
r.sims <- matrix(runif(52)*100, nrow=52, ncol=100)
e.sims <- matrix(runif(52)*100, nrow=52, ncol=100)
ma1 <- 1
ar1 <- 2
ar2 <- 3
for(j in 1:1000) {
for(i in 1:52) {
if (i == 1) {
r.sims[i,] <- r.sims[1]
} else {
if(i == 2) {
r.sims[i,] <- r.sims[2]
} else {
if(i > 2) {
r.sims[i,] <- r.sims[i-1,]*ar1 + r.sims[i-2,]*ar2 + e.sims[i,] + e.sims[i-1,]*ma1
}
}
}
}
}
You seem to misunderstand the concept of else.
else covers all cases that didn't match previous if or else if statements.
Therefore, you cannot specify any condition for else.
To cover all cases where i is not 1 or 2 simply use else, without any () brackets.
If you want to have a condition, use else if (condition).

Resources