I have a code that is failing to work because of curly bracket issues
o1 = read.table('/Users/manshi/Desktop/PSYC57H3/PSYC57_Homework3/Object1.csv', header=TRUE, sep=",")
PredictedValue = vector(mode = 'numeric', length = 100)
PredictionError = vector(mode = 'numeric', length = 100)
PredictedValue[1] = 0
AlAccepts = vector(mode = 'logical', length = 100)
for (trial in 1:100){
ifelse (AlAccepts[trial] == FALSE, 0, 1) {
PredictionError[trial] = o1$Reward[trial] - PredictedValue[trial]
PredictedValue[trial + 1] = PredictedValue[trial] + .3*PredictionError[trial]
} ifelse (AlAccepts[trial] == TRUE, 0, 1) {
PredictedValue[trial + 1] = PredictedValue[trial]
}
}
The error message that I am getting is:
> for (trial in 1:100){
+ ifelse (AlAccepts[trial] == FALSE, 0, 1) {
Error: unexpected '{' in:
"for (trial in 1:100){
ifelse (AlAccepts[trial] == FALSE, 0, 1) {"
> PredictionError[trial] = o1$Reward[trial] - PredictedValue[trial]
> PredictedValue[trial + 1] = PredictedValue[trial] + .3*PredictionError[trial]
> } ifelse (AlAccepts[trial] == TRUE, 0, 1) {
Error: unexpected '}' in " }"
> PredictedValue[trial + 1] = PredictedValue[trial]
> }
Error: unexpected '}' in " }"
> }
Error: unexpected '}' in "}"
>
What am I doing wrong?
ifelse (AlAccepts[trial] == FALSE, 0, 1) is a function and results in a return value, in this case 0 or 1. Further conditioned excecution is not done so the following brackets show a syntax error. Using else if(){} should work as you intended:
for (trial in 1:100){
if (AlAccepts[trial] == FALSE) {
PredictionError[trial] = o1$Reward[trial] - PredictedValue[trial]
PredictedValue[trial + 1] = PredictedValue[trial] + .3*PredictionError[trial]
} else if(AlAccepts[trial] == TRUE) {
PredictedValue[trial + 1] = PredictedValue[trial]
}
}
Related
When running the code below I get the error:
Error in data[, 4] : incorrect number of dimensions
Both data[,4] and goals have the same length (480) so I don't understand what the issue is. Data is a data.frame with 4 columns and goals is a length 480 vector.
library(glmmTMB)
simulate_games = function(data) {
mod <- glmmTMB(goals ~ home + (1|attack) + (1|defence), poisson, data=data, REML=TRUE)
goals = predict(mod,newdata = data, type = "response")
data[,4] = goals #Error here
res = comp_ranks(goals)[,2] #comp_ranks is a user defined function
for (i in 1:1000) {
data[,4] = rpois(480,goals)
res = cbind(res,comp_ranks(data)[,2])
}
return(res)
}
long <- read.csv("https://www.math.ntnu.no/emner/TMA4315/2020h/eliteserie.csv", colClasses = c("factor","factor","factor","numeric"))
simulate_games(long)
Here is also the comp_ranks function although I don't think its whats causing the error.
comp_ranks = function(data) {
goals = data[,4]
goals = goals[!is.na(goals)]
teams = unique(data[,1])
teams_points = cbind.data.frame(0,teams)
goals_scored = cbind.data.frame(0,teams)
goals_conceded = cbind.data.frame(0,teams)
for (i in 1:length(teams)) {
tfs = data[,1] == teams[i]
tfc = data[,2] == teams[i]
goals_scored[i,1] = sum(na.omit(goals[tfs]))
goals_conceded[i,1] = sum(na.omit(goals[tfc]))
}
for (i in seq(1,length(goals)-1,2)) {
idx_1 = match(data[,1][i],teams)
idx_2 = match(data[,1][i+1],teams)
if (goals[i] - goals[i+1] > 0) {
teams_points[idx_1,1] = teams_points[idx_1,1] + 3
}
else if (goals[i] - goals[i+1] < 0 ) {
teams_points[idx_2,1] = teams_points[idx_2,1] + 3
}
else {
teams_points[idx_1,1] = teams_points[idx_1,1] + 1
teams_points[idx_2,1] = teams_points[idx_2,1] + 1
}
}
#Sort data.frame by ranks
colnames(teams_points) = c("Points","Teams")
teams_points = teams_points[with(teams_points, order(-Points)), ]
diff = goals_scored[,1] - goals_conceded[,1]
goals_diff = cbind.data.frame(diff,teams)
teams_ranked = teams_points[,2]
for (i in 1:length(teams_points)) {
for (j in 1:length(teams_points)) {
if(j != i) {
if (teams_points[i,1] == teams_points[j,1]) {
if (goals_diff[i,1] == goals_diff[j,1]) {
if (goals_scored[i,1] < goals_scored[j,1] ) {
teams_ranked = replace(teams_ranked,c(i,j), teams_ranked[c(j,i)])
teams_points[,2] = teams_ranked
}
else if(goals_diff[i,1] < goals_diff[j,1] ) {
teams_ranked = replace(teams_ranked,c(i,j), teams_ranked[c(j,i)])
teams_points[,2] = teams_ranked
}
}
}
}
}
}
ranks = data.frame("Ranks" = c(1:16), "Teams" = teams_points[,2], "Points" = teams_points[,1])
return(ranks)
}
im new to R and im trying to solve for the minimum number of moves for a knight visit all the moves in a chess board.
I got the python code from:
https://www.geeksforgeeks.org/the-knights-tour-problem-backtracking-1/
and i tried to translate it to r.
But i am always getting the error and I don't know where I went wrong.
This is my code:
chess = rep(-1, times = 64)
board = matrix(data = chess, nrow = 8, ncol = 8, byrow = TRUE)
move_x = c(2, 1, -1, -2, -2, -1, 1, 2)
move_y = c(1, 2, 2, 1, -1, -2, -2, -1)
board[1, 1] = 0
pos = 1
valid_move <- function (x, y, board) {
if (x >= 1 & y >= 1 & x <= 8 & y <= 8 & board[x, y] == -1) {
return (T)
}
return (F)
}
solve <- function (board, curr_x, curr_y, move_x, move_y, pos) {
if (pos == 64) {
return (T)
}
for (i in seq(1:8)) {
new_x = curr_x + move_x[i]
new_y = curr_y + move_y[i]
if (valid_move(new_x, new_y, board)) {
board[new_x, new_y] = pos
if (solve(board, new_x, new_y, move_x, move_y, pos+1)) {
return (TRUE)
}
board[new_x, new_y] = -1
}
}
}
main <- function() {
sims = 10
ctr = 0
number_of_moves = c()
solve(board, 1, 1, move_x, move_y, pos)
print(paste("Minimum number of moves: ", pos))
}
main()
Thanks!
The problem is that the python code relies on short-circuiting to prevent out-of-bounds errors. & will not short-circuit so you need to use &&.
Here is an example
FALSE && stop()
#> [1] FALSE
FALSE & stop()
#> Error:
Update valid_move to this
valid_move <- function (x, y, board) {
# Changed to && to allow short-circuiting
# if (x >= 1 & y >= 1 & x <= 8 & y <= 8 & board[x, y] == -1) {
if (x >= 1 && y >= 1 && x <= 8 && y <= 8 && board[x, y] == -1) {
return (T)
}
return (F)
}
Hi I'm trying to make a server in R shiny, I have the following code:
output$map <- renderTmap( {
cat("renderTmap (initialise map) | ")
if (input$varID == "Temperture") {
tm_basemap(c("Esri.OceanBasemap","CartoDB.DarkMatter","OpenStreetMap.Mapnik"),alpha = 0.7) +
weLayerF(weather,"temp", "feel_like" ,"-RdYlBu", seq(from = 0, to = 45, by = 2))
} else if (input$varID == "humidity") {
tm_basemap(c("Esri.OceanBasemap","CartoDB.DarkMatter","OpenStreetMap.Mapnik"),alpha = 0.7) +
weLayer(weather,"humidity", "YlOrRd", seq(from = 0, to = 100, by = 2))
} else if (input$varID == "Pressure") {
tm_basemap(c("Esri.OceanBasemap","CartoDB.DarkMatter","OpenStreetMap.Mapnik"),alpha = 0.7) +
weLayer(weather,"pressure", "PuBu", seq(from = 980, to = 1030, by = 2))
} else if (input$varID == "Visablity") {
tm_basemap(c("Esri.OceanBasemap","CartoDB.DarkMatter","OpenStreetMap.Mapnik"),alpha = 0.7) +
weLayer(weather,"visib", "-Greys", seq(from = 0, to = 10000, by = 500))
} else if (input$varID == "Wind") {
tm_basemap(c("Esri.OceanBasemap","CartoDB.DarkMatter","OpenStreetMap.Mapnik"),alpha = 0.7) +
weLayerF(weather,"wind_speed","wind_degree", "Greys", seq(from = 0, to = 30, by = 2))
} else {
tm_basemap(c("Esri.OceanBasemap","CartoDB.DarkMatter","OpenStreetMap.Mapnik"),alpha = 0.7) +
weLayerF(weather,"temp", "feel_like" ,"-RdYlBu", seq(from = -10, to = 45, by = 5))
}
})
And I'm getting the error:
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
Can someone help please?
doubt image
for(i in 1:rowcount){
t = sum(temp[i,])
if(t>0){
rowmean = mean(temp[i,])
rowsd = sd(temp[i,])*sqrt(colcount-1)
temp[i,] = (temp[i,]-rowmean)/rowsd
}
}
requiredperson =4661 #Obama
dotproducts = numeric(rowcount)
for( i in 1:rowcount){
dotproducts[i] = sum(temp[requiredperson,]*temp[i,])
}
Error in temp[requiredperson, ] : subscript out of bounds
My goal is to call my dice roll function n times where n is the amount of turns. This is a monopoly simulated turn and as such the doubles will roll again, and triples will go to jail. I cannot figure out how to set this up so my function will be
Diceroll <- Function ( Turns, Sides)
Diceroll <- function(Turn,sides){
Turn = as.integer(0)
First_roll = as.integer(0)
Second_roll = as.integer(0)
Third_roll = as.integer(0)
Fourth_roll = as.integer(0)
Fifth_roll = as.integer(0)
Sixth_roll = as.integer(0)
Total = as.integer(0)
i = as.integer(1)
for (i in 1:Turn) {
First_roll = sample(1:sides,size = 1)
Second_roll = sample(1:sides,size = 1)
if(First_roll[1] == Second_roll[1]) {
Third_roll = sample(1:sides,size = 1)
Fourth_roll = sample(1:sides,size = 1)
}
if(Third_roll[1] == Fourth_roll[1] & Third_roll[1] + Fourth_roll[1] > 0) {
Fifth_roll= sample(1:sides,size = 1)
Sixth_roll = sample(1:sides,size = 1)
}
if(Fifth_roll[1] == Sixth_roll[1] & Fifth_roll[1] + Sixth_roll[1] > 0) { Total = "Jail"
}
else {
Total = (First_roll[1] + Second_roll[1] + Third_roll[1] +
Fourth_roll[1] + Fifth_roll[1] + Sixth_roll[1]) }
return(Total)
}
}
Here is my attempt but it is only listed the value of one roll.