I have a vector of start and end angels values between 0 and 360.
I would like to have an additional column that specifies in which sector(consider 12 sectors) are my variables.
sectors should be defined as : (15:45], (45,75],(75,105], ..., (345,15]
test = structure(list(start = c(4, 67, 13, 35, 54, 0), end = c(23, 84,
30, 52, 71, 0)), row.names = 2:7, class = "data.frame")
For my test example I thought I have to loop over the number of rows :
for( i in 1:nrow(test)){
if(test$start[i] <= 15 | test$start[i] >345){test$sector_start[i] = 12}
else if(test$start[i] > 15 & test$start[i] <= 45){test$sector_start[i] = 1}
else if(test$start[i] > 45 & test$start[i] <= 75){test$sector_start[i] = 2}
else if(test$start[i] > 75 & test$start[i] <= 105){test$sector_start[i] = 3}
else if(test$start[i] > 105 & test$start[i] <= 135){test$sector_start[i] = 4}
else if(test$start[i] > 135 & test$start[i] <= 165){test$sector_start[i] = 5}
else if(test$start[i] > 165 & test$start[i] <= 195){test$sector_start[i] = 6}
else if(test$start[i] > 195 & test$start[i] <= 225){test$sector_start[i] = 7}
else if(test$start[i] > 225 & test$start[i] <= 255){test$sector_start[i] = 8}
else if(test$start[i] > 255 & test$start[i] <= 285){test$sector_start[i] = 9}
else if(test$start[i] > 285 & test$start[i] <= 315){test$sector_start[i] = 10}
else if(test$start[i] > 315 & test$start[i] <= 345){test$sector_start[i] = 11}
if(test$end[i] <= 15 | test$end[i] >345){test$sector_end[i] = 12}
else if(test$end[i] > 15 & test$end[i] <= 45){test$sector_end[i] = 1}
else if(test$end[i] > 45 & test$end[i] <= 75){test$sector_end[i] = 2}
else if(test$end[i] > 75 & test$end[i] <= 105){test$sector_end[i] = 3}
else if(test$end[i] > 105 & test$end[i] <= 135){test$sector_end[i] = 4}
else if(test$end[i] > 135 & test$end[i] <= 165){test$sector_end[i] = 5}
else if(test$end[i] > 165 & test$end[i] <= 195){test$sector_end[i] = 6}
else if(test$end[i] > 195 & test$end[i] <= 225){test$sector_end[i] = 7}
else if(test$end[i] > 225 & test$end[i] <= 255){test$sector_end[i] = 8}
else if(test$end[i] > 255 & test$end[i] <= 285){test$sector_end[i] = 9}
else if(test$end[i] > 285 & test$end[i] <= 315){test$sector_end[i] = 10}
else if(test$end[i] > 315 & test$end[i] <= 345){test$sector_end[i] = 11}
}
here I could add 2 column to test which is telling me my angles are in which sector. I'm looking for a smarter way of doing this that I could have an option to vary the number of sectors, for example to 24 sectors.
As Roman says, you can use cut. The last step is for angles > 345 or <= 15.
library(dplyr)
test %>%
mutate(sector_start = cut(start, 15 + 30*(0:11), 1:11)
, sector_end = cut(end, 15 + 30*(0:11), 1:11)) %>%
mutate_at(vars(contains('sector')), ~ifelse(is.na(.), 12, .))
In base R:
test[paste0('sector_', names(test))] <-
lapply(test, function(x){
labs <- cut(x, 15 + 30*(0:11), 1:11)
ifelse(is.na(labs), 12, labs)
})
Related
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)
}
I'm doing an IF statement in R to create a variable in R.
I'm having an error that I can't detect to what it refers exactly so I can't fix it. Can somebody help me?
library(install.load)
install_load("checkmate", "expss")
amostra$escol <- NA
educd003 <- data.frame("d003" = 1:9, "codeduc" = c(1,1,3,3,5,5,7,9,9))
educd0091 <- data.frame("d009" = c(1,2,3,4,5,6,7,8,9,10,11,12),
"codeduc" = c(2,2,4,4,4,4,6,6,6,8,10,10))
educd0092 <- data.frame("d009" = c(1,2,3,4,5,6,7,8,9,10,11,12),
"codeduc" = c(1,1,3,3,3,3,5,5,5,7,9,9))
for (i in 1:nrow(amostra)) {
if (is.na(amostra$d001[i]) == TRUE) {
amostra$escol[i] <- 99
} else if (amostra$d001[i] == 2) {
amostra$escol[i] <- 0
} else if (amostra$d002[i] == 1) {
amostra$escol[i] <- vlookup(amostra$d003[i], educd003, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 2) {
amostra$escol[i] <- 2
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == 1) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0091, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == 2) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0092, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & is.na(amostra$d014[i]) == TRUE) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0092, result_column = 2, lookup_column = 1)
} else {
amostra$escol[i] <- NA
}
}
Error:
Error in if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == :
missing value where TRUE/FALSE needed
Thanks,
Wagner
I solved it.
The problem, apparently, was the order.
The code below ran ok:
for (i in 1:nrow(amostra)) {
if (is.na(amostra$d001[i]) == TRUE) {
amostra$escol[i] <- 99
} else if (amostra$d001[i] == 2) {
amostra$escol[i] <- 0
} else if (amostra$d002[i] == 1) {
amostra$escol[i] <- vlookup(amostra$d003[i], educd003, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 2) {
amostra$escol[i] <- 2
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & is.na(amostra$d014[i]) == TRUE) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0092, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == 1) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0091, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == 2) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0092, result_column = 2, lookup_column = 1)
} else {
amostra$escol[i] <- NA
}
}
Thank you very much anyway!
I am trying to find the probability that the chain jumps from state k-1 to state 1 before it hits state k.
Can anyone spot my mistake?
I tried to simulate the markov chain, but i want to make a code that allows me to find probability of k ={1, 2, 3, ........17}. But I can really not get the code.
This is the error message I always get
Error in while (X[i] > 1 && X[i] < k) { :
missing value where TRUE/FALSE needed
Here is my code:
k <- 17
{ p <- 0.5
q <- 0.1
P <- matrix (0, nrow = k, ncol = k, byrow = TRUE)
for (i in 1:k)
{ for (j in 1:k)
{ if (i == 1 && i == j)
{ P[i,j] <- 1
}
else if (i == k && i == j)
{ P[i,j] <- 1
}
else if (i == j)
{ P[i,j] <- p*(1-q)
}
else if (j == k && i != 1)
{ P[i,j] <- q
}
else if (i == j+1 && i != k)
{ P[i,j] <- (1-p)*(1-q)
}
}
}
P
X <- (k-1)
trials <- 1000
hits <- 0 #counter for no. of hits
for (i in 1:trials)
{ i <- 1 #no. of steps
while(X[i] > 1 && X[i] < k)
{ Y <- runif(1) #uniform samples
p1 <- P[X[i],] #calculating the p-value
p1 <- cumsum(p1)
# changes in the chain
if(Y <= p1[1])
{ X[i+1] = 1}
else if(Y <= p1[2])
{ X[i+1] = 2}
else if(Y <= p1[3])
{ X[i+1] = 3}
else if(Y <= p1[4])
{ X[i+1] = 4}
else if(Y <= p1[5])
{ X[i+1] = 5}
else if(Y <= p1[6])
{ X[i+1] = 6}
else if(Y <= p1[7])
{ X[i+1] = 7}
else if(Y <= p1[8])
{ X[i+1] = 8}
else if(Y <= p1[9])
{ X[i+1] = 9}
else if(Y <= p1[10])
{ X[i+1] = 10}
else if(Y <= p1[11])
{ X[i+1] = 11}
else if(Y <= p1[12])
{ X[i+1] = 12}
else if(Y <= p1[13])
{ X[i+1] = 13}
else if(Y <= p1[14])
{ X[i+1] = 14}
else if(Y <= p1[15])
{ X[i+1] = 15}
else if(Y <= p1[16])
{ X[i+1] = 16}
else if(Y <= p1[17])
{ X[i+1] <= 17}
i <- i+1
}
if(X[i]==1)
{ hits <- hits+1}
else
{ hits <- hits+0}
}
Probability <- hits/trials
Probability
}
I think the line
i <- 1 #no. of steps
should not be there. Try this:
k <- 17
{ p <- 0.5
q <- 0.1
P <- matrix (0, nrow = k, ncol = k, byrow = TRUE)
for (i in 1:k)
{ for (j in 1:k)
{ if (i == 1 && i == j)
{ P[i,j] <- 1
}
else if (i == k && i == j)
{ P[i,j] <- 1
}
else if (i == j)
{ P[i,j] <- p*(1-q)
}
else if (j == k && i != 1)
{ P[i,j] <- q
}
else if (i == j+1 && i != k)
{ P[i,j] <- (1-p)*(1-q)
}
}
}
P
X <- (k-1)
trials <- 1000
hits <- 0 #counter for no. of hits
for (i in 1:trials)
{
while(X[i] > 1 && X[i] < k)
{ Y <- runif(1) #uniform samples
p1 <- P[X[i],] #calculating the p-value
p1 <- cumsum(p1)
# changes in the chain
if(Y <= p1[1])
{ X[i+1] = 1}
else if(Y <= p1[2])
{ X[i+1] = 2}
else if(Y <= p1[3])
{ X[i+1] = 3}
else if(Y <= p1[4])
{ X[i+1] = 4}
else if(Y <= p1[5])
{ X[i+1] = 5}
else if(Y <= p1[6])
{ X[i+1] = 6}
else if(Y <= p1[7])
{ X[i+1] = 7}
else if(Y <= p1[8])
{ X[i+1] = 8}
else if(Y <= p1[9])
{ X[i+1] = 9}
else if(Y <= p1[10])
{ X[i+1] = 10}
else if(Y <= p1[11])
{ X[i+1] = 11}
else if(Y <= p1[12])
{ X[i+1] = 12}
else if(Y <= p1[13])
{ X[i+1] = 13}
else if(Y <= p1[14])
{ X[i+1] = 14}
else if(Y <= p1[15])
{ X[i+1] = 15}
else if(Y <= p1[16])
{ X[i+1] = 16}
else if(Y <= p1[17])
{ X[i+1] <= 17}
i <- i+1
}
if(X[i]==1)
{ hits <- hits+1}
else
{ hits <- hits+0}
}
Probability <- hits/trials
Probability
}
You're setting X to k-1. In R, that's treated as a vector of length 1. As soon as i reaches 2, X[i] return an index error, because X does not have a second element.
Further notes: using the same index in two different nesting levels is very bad form. Also, when you start having a massive list of if-then-else statements, it's time to rethink your code. In this case, you could just subset 1:17 on p1[i] >=Y, take the minimum value, and then set X to that.
I am working on a data frame with x and y columns with values as rows.. I want to calculate the slope of x and y for every 2 rows and then using the calculated slope, record whether slope's "stability" is "high" or "low". You'll understand better after seeing the code. What is wrong with this code? When I input stability, R returns NULL.
slope <- (acc$y[i+1] - acc$y[i]) / (acc$x[i+1] - acc$x[i])
stability <- c()
for (i in 1:nrow(acc)) {
if (slope[i] > 0 & slope[i] < 0.8) {
stability[i] <- "low"
} else if (slope[i] >= 0.8 & slope[i] <= 1) {
stability[i] <- "high"
} else if (slope[i] > 1 & slope[i] < 1.2) {
stability[i] <- "high"
} else if (slope[i] >= 1.2) {
stability[i] <- "low"
} else if (slope[i] >= -1 & slope[i] <= -0.8) {
stability[i] <- "high"
} else if (slope[i] >= -0.8 & slope[i] <= 0) {
stability[i] <- "low"
} else if (slope[i] < -1 & slope[i] > -1.2) {
stability[i] <- "high"
} else
stability[i] <- "low"
}
Modify your code as following:
#slope <- (acc$y[i+1] - acc$y[i]) / (acc$x[i+1] - acc$x[i])
stability <- as.vector(nrow(acc))
n <- nrow(acc)-1
for (i in 1:n) {
slope <- (acc$y[i+1] - acc$y[i]) / (acc$x[i+1] - acc$x[i])
if (slope > 0 && slope < 0.8) {
stability[i] <- "low"
} else if (slope >= 0.8 && slope <= 1) {
stability[i] <- "high"
} else if (slope > 1 && slope < 1.2) {
stability[i] <- "high"
} else if (slope >= 1.2) {
stability[i] <- "low"
} else if (slope >= -1 && slope <= -0.8) {
stability[i] <- "high"
} else if (slope >= -0.8 && slope <= 0) {
stability[i] <- "low"
} else if (slope < -1 && slope > -1.2) {
stability[i] <- "high"
} else
stability[i] <- "low"
}
Try it Online
I need someone to tell me how to write a code easy and understandable for this matrix
In this exercise is search to delete a row and a column:
const int n = 4, m = 4;
int A[n][m] = { { 2, -8, -7, 5 },{ 4, -7, -8, 2 },{ 1, 10, 3, 6 },{ 4, 7, 9, -3 } };
int i, j, r, k, B[n][m];
variable r stands for row which need to be deleted
variable k stands for column which needs to be deleted
B[n][m] stands for new matrix that will be showed on console
Waiting for answer
there's my code :
const int n = 4, m = 4;
int A[n][m] = { { 2, -8, -7, 5 },
{ 4, -7, -8, 2 },
{ 1, 10, 3, 6 },
{ 4, 7, 9, -3 } };
int i, j, r, k, B[n][m];
cout << "give a value r and k:";
cin >> r >> k;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if ((i < r) && (j < k))
B[i][j] = A[i][j];
if ((i >= r) && (j < k))
B[i][j] = A[i + 1][j];
if ((i < r) && (j >= k))
B[i][j] = A[i][j + 1];
if ((i >= r) && (j >= k))
B[i][j] = A[i + 1][j + 1];
}
}
cout << "Matrix B ={" << endl;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < m - 1; j++)
{
cout.width(3);
cout << B[i][j];
}
cout << endl;
but here is what mix up things to me :
if ((i < r) && (j < k))
B[i][j] = A[i][j];
if ((i >= r) && (j < k))
B[i][j] = A[i + 1][j];
if ((i < r) && (j >= k))
B[i][j] = A[i][j + 1];
if ((i >= r) && (j >= k))
B[i][j] = A[i + 1][j + 1];
}
this is the part where i mess up more often
Can somebody tell me more easier way ?