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

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')
}
}

Related

Creating a random play function in R

I want to create a gambling game that does the following:
It is a lottery, which asks for 5 numbers comprised between 01 and 43 and an additional number comprised between 01 and 16; the idea is that I give my 6 numbers and it tells me if I won or not.
To do so, use this code
Lotery = function(a,b,c,d,e,f){
NumeroAleatorio <- matrix(1:6, ncol = 6)
NumeroAleatorio[1] <- sample (1:43, 1, replace= FALSE)
for (i in 2:6) {
if(i!=6){
NumeroAleatorio[i] <- sample (1:43, 1, replace= FALSE)
if(i == 2){
while(NumeroAleatorio[i] == NumeroAleatorio[1] ){
NumeroAleatorio[i] <- sample (1:43, 1, replace= FALSE)
}
}else{
if(i == 3 ){
while(NumeroAleatorio[i] == NumeroAleatorio[1] || NumeroAleatorio[i] == NumeroAleatorio[2]){
NumeroAleatorio[i] <- sample (1:43, 1, replace= FALSE)
}
}else{
if(i == 4 ){
while(NumeroAleatorio[i] == NumeroAleatorio[1] || NumeroAleatorio[i] == NumeroAleatorio[2] || NumeroAleatorio[i] == NumeroAleatorio[3]){
NumeroAleatorio[i] <- sample (1:43, 1, replace= FALSE)
}
}else{
if(i == 5 ){
while(NumeroAleatorio[i] == NumeroAleatorio[1] || NumeroAleatorio[i] == NumeroAleatorio[2] || NumeroAleatorio[i] == NumeroAleatorio[3] || NumeroAleatorio[i] == NumeroAleatorio[4] ){
NumeroAleatorio[i] <- sample (1:43, 1, replace= FALSE)
}
}
}
}
}
}else{
NumeroAleatorio[6] <- sample (1:16, 1, replace= FALSE)
}
}
ContA<-0
ContB<-0
ContC<-0
ContD<-0
ContE<-0
ContF<-0
for(p in 1:6){
if(NumeroAleatorio[p] == a){
ContA<-ContA+1
}
if(NumeroAleatorio[p] == b){
ContB<-ContB+1
}
if(NumeroAleatorio[p] == c){
ContC<-ContC+1
}
if(NumeroAleatorio[p] == d){
ContD<-ContD+1
}
if(NumeroAleatorio[p] == e){
ContE<-ContE+1
}
if(NumeroAleatorio[p] == f){
ContF<-ContF+1
}
}
if(ContA==0 || ContB==0 || ContC==0 || ContD==0 || ContE==0 || ContF==0){
return(data.frame("Resultado", NumeroAleatorio, "Numero escogido", a, b, c, d, e, f, "No Ganaste"))
}else{
return(data.frame("Resultado", NumeroAleatorio, "Numero escogido", a, b, c, d, e, f, "Ganaste"))
}
}
Lotery(20,15,3,45,9,8)
It seems to work, but I want to know if I can make things simpler because I think the code is too long.
I may not be following your code, but it looks like you randomly select 6 numbers and compare them to the numbers submitted. None of the first 5 should match, but the 6th can match one of the first 5. If that is correct then this will generate the random 6 numbers:
NumeroAleatorio <- c(sample.int(43, 5, replace=FALSE), sample.int(16, 1))
To compare this to the submitted values, just use
NumeroAleatorio == comp
All FALSE indicates no matches. Any TRUE values indicate a match at that position.

R - Error executing user-defined function

I am trying to build a function that takes 2 arguments and uses those 2 arguments inside a replicate funtion
SPM <- function(bilhetes, N){
total_bilhetes <- 12012000
total_bilhetes_premios <- 3526450
premios <- c(0,5,10,15,20,25,50,100,300,1000,27000,108000,288000)
premios_bilhetes <- c(8485550,1895000,496800*2,88800*3,55200*4,16800*4,7920*6,5030*6,950*5,950,30,10,10)
probs <- premios_bilhetes/total_bilhetes
vector_ganhos <- c()
ganho <- 0
replicate(N, function(bilhetes) {
total_bilhetes1 <- total_bilhetes
premios_bilhetes1 <- premios_bilhetes
probs1 <- probs
for (i in c(1:bilhetes)) {
A <- sample(x = premios,replace = T,size = 1, prob = probs1)
ganho <- ganho - 5 + A
if (A == 0) {
premios_bilhetes1[1] <- premios_bilhetes1[1] - 1
} else if (A == 5) {
premios_bilhetes1[2] <- premios_bilhetes1[2] - 1
} else if (A == 10) {
premios_bilhetes1[3] <- premios_bilhetes1[3] - 1
} else if (A == 15) {
premios_bilhetes1[4] <- premios_bilhetes1[4] - 1
} else if (A == 20) {
premios_bilhetes1[5] <- premios_bilhetes1[5] - 1
} else if (A == 25) {
premios_bilhetes1[6] <- premios_bilhetes1[6] - 1
} else if (A == 50) {
premios_bilhetes1[7] <- premios_bilhetes1[7] - 1
} else if (A == 100) {
premios_bilhetes1[8] <- premios_bilhetes1[8] - 1
} else if (A == 300) {
premios_bilhetes1[9] <- premios_bilhetes1[9] - 1
} else if (A == 1000) {
premios_bilhetes1[10] <- premios_bilhetes1[10] - 1
} else if (A == 27000) {
premios_bilhetes1[11] <- premios_bilhetes1[11] - 1
} else if (A == 108000) {
premios_bilhetes1[12] <- premios_bilhetes1[12] - 1
} else {
premios_bilhetes1[13] <- premios_bilhetes1[13] - 1
}
total_bilhetes1 <- total_bilhetes1 - 1
probs1 <- premios_bilhetes1/(total_bilhetes1)
}
vector_ganhos[length(vector_ganhos)+1] = ganho
})
return(vector_ganhos)
}
when I try to run it, e.g., SPM(bilhetes = 5, N = 100) I get:
Error in SPM(bilhetes = 5, N = 100) : could not find function "SPM"
I looked in another question and someone mentioned "sourcing" the function. I tried it, and this was the output:
> source("SPM")
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
I'm rather new to R, so I'm probably making a dumb mistake.
Can someone help?

R guessing game syntax

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
}

If & ifelse statement in R

As the new user in R, I met few problems when I tried to evaluate a.
Code:
// time2 are numbers //
a = d3$Time2
b = c(...)
for (i in 1:65){
for (j in 1:1762){
if( (a[j]>=1474161415+900*(i-1))&(a[j]<1474161415+900*i) ){ a[j] = b[j] }
}
}
Results:
Error in if ((a[j] >= 1474161415 + 900 * (i - 1)) & (a[j] < 1474161415 + :
missing value where TRUE/FALSE needed
Also I have tried:
ifelse( ((a[j]>=1474161415+900*(i-1)) & (a[j]<1474161415+900*i)) , a[j]=b[j])
Results:
-unexpected '=' in -ifelse( ((a[j]-=1474161415+900-(i-1)) &
(a[j]-1474161415+900-i)) , a[j]=--

Use result of previous loop as input for next loop

I have the code below, which seems to accomplish what I'm trying to do but also throws the error output shown below the code. What I'm trying to do, is run through the loop the first time with x = 1, then for each time the loop runs after that I want x = y, the result of the previous loop. I always fumble with loops so any tips are greatly appreciated.
Code:
for(i in 1:5)
{
if(i=1)
{
x<-1
}
else
{
x<-y
}
y<-x*i
y
}
ERRORS:
for(i in 1:5)
+ {
+ if(i=1)
Error: unexpected '=' in:
"{
if(i="
> {
+ x<-1
+ }
> else
Error: unexpected 'else' in " else"
> {
+ x<-y
+ }
> y<-x*i
> y
[1] 25
> }
Error: unexpected '}' in "}"
Here is your code re-written with slightly clearer syntax
for (i in 1:5) {
if (i == 1) {
x <- 1
} else {
x <- y
}
y <- x * i
}
Or even better syntax.
for (i in 1:5) {
x <- ifelse(i == 1, 1, y)
y <- x * i
}

Resources