While loop missing value where TRUE/FALSE needed - r

simulate_queue<- function(open_time, rate, low, high){
i<-1
j<-1
a<-rexp(1,rate)
b<-runif(1,low,high)
c<-a+b
d<-a + rexp(1,rate)
e<-c()
while (d <= open_time) {
a[i+1]<- d
if (a[i+1] < c[i]){
c[i+1]<-c[i]+b
b<- runif(1,low,high)
}
else c[i+1]<-a[i+1]+b
e[i]<-( while(d <= open_time){
if (c[j] < a[i+1]) {sum(c[j]< a[i])
i <- i+1
}
else if (c[j] > a[i+1]) {0
j <- j+1
}
else 0
}
)
i<-i+1
d <- a[i] + rexp(1,rate)
}
L <- list(arrival_times=a,queue_lengths=e,departure_times=c)
L
}
I don't know why there is an error message
Error in if (c[j] < a[i + 1]) { : missing value where TRUE/FALSE needed
Please help me..

Related

How to iterate over 1 <= i < j <= n elements in R?

So I am trying to do a for loop over the upper triangle part of a matrix, so I only want the elements 1 <= i < j <= n. And I tried it out in R as follows:
for(i in 1:n-1) {
for(j in i+1:n) {
...
}
}
But instead of iterating over 1 <= i < j <= n these for loops go over the elements i + 1 <= j <= i + n, 1 <= i < n.
I'm new to R, so I don't understand what is happening. Could someone give me a hint how to do it correctly?
Thanks.
for(i in seq(1, n - 1)) {
for(j in seq(i + 1, n)) {
...
}
}
alternatively
for(i in 1:(n - 1)) {
for(j in (i + 1):n) {
...
}
}
The issue is that R understands i+1:n as i + (1:n)

If loop - Argument is of length zero - error

I do have the following code for a loop, which basically only updates the column "decay rate" based on whether or not the column "RPK_cap" is higher than a certain threshold which is exogenously given(in scenario 1 it is 1200, in Scenario 2 it is 1300 etc). If that is the case than I want to decrease the decay rate by multiplying it my 0.9. This is done for all countries (column: iso) and years(column: year) which are in the data.table. When I am using scenario 1 data the column "RPK_cap" will be filled with scenario 1 data and if I choose scenario 2 the column "RPK_cap" will be filled with scenario 2 data respectively.
And my problem is the following:
This loop works ONLY for scenario 1.
If I am using scenario 2 I get the error message:
"Error in if (price_el_int_aviation_RPK$RPK_Cap[price_el_int_aviation_RPK$iso == :
argument is of length zero"
I really tried a lot and I just donĀ“t know what the problem is here. Does anyone know what the problem is?
This is the data.table
data.table_example
if (scenario == "1") {
for (j in unique(price_el_int_aviation_RPK$iso)) {
for (i in unique(price_el_int_aviation_RPK$year[price_el_int_aviation_RPK$iso == j])) {
if (price_el_int_aviation_RPK$RPK_cap[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year == i] > 1200) {
price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] <- price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] * 0.90
}
}
}
} else if (scenario == "2"){
for (j in unique(price_el_int_aviation_RPK$iso)) {
for (i in unique(price_el_int_aviation_RPK$year[price_el_int_aviation_RPK$iso == j])) {
if (price_el_int_aviation_RPK$RPK_Cap[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year == i] > 1300) {
price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] <- price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] * 0.95
}
}
}
} else if (scenario == "3"){
for (j in unique(price_el_int_aviation_RPK$iso)) {
for (i in unique(price_el_int_aviation_RPK$year[price_el_int_aviation_RPK$iso == j])) {
if (price_el_int_aviation_RPK$RPK_Cap[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year == i] > 1400) {
price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] <- price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] * 0.95
}
}
}
} else if (scenario == "4"){
for (j in unique(price_el_int_aviation_RPK$iso)) {
for (i in unique(price_el_int_aviation_RPK$year[price_el_int_aviation_RPK$iso == j])) {
if (price_el_int_aviation_RPK$RPK_Cap[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year == i] > 1500) {
price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] <- price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] * 0.95
}
}
}
} else if (scenario == "5"){
for (j in unique(price_el_int_aviation_RPK$iso)) {
for (i in unique(price_el_int_aviation_RPK$year[price_el_int_aviation_RPK$iso == j])) {
if (price_el_int_aviation_RPK$RPK_Cap[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year == i] > 1600) {
price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] <- price_el_int_aviation_RPK$decay_rate[price_el_int_aviation_RPK$iso == j & price_el_int_aviation_RPK$year >= i] * 0.99
}
}
}
}else{}
It's hard to answer in specifics because we cannot see your data.
But the error you see
"Error in if (price_el_int_aviation_RPK$RPK_Cap[price_el_int_aviation_RPK$iso == : argument is of length zero"
is because price_el_int_aviation_RPK$iso is of length zero - it's a vector without anything in it!
Here's a minimal example of the problem
# Create a vector of length zero
a <- c()
# Prove it's length is zero
length(a)
# [1] 0
# This is a minimal example of what's going wrong with scenario 2 in your script
if(a == 2) { print("it works") }
Error in if (a == 2) if (a == 2) { : argument is of length zero

Loop returns error: 'argument is of length zero'

i <- 2
j <- 0
for (i in 2:1000) {
if(return.prime(i)){j = j + 1}
i = i + 1
}
I want to check how many prime numbers there are in 1 to 1000 using my own function return.prime which returns TRUE when the number input is a prime and FALSE when the number input is not prime. The return.prime function is the function below and it is correct.
return.prime <- function(d){
if(d ==1 ){print(FALSE)}
if (d == 2){
print(TRUE)
}
if(d !=2 && d!=1){
if(any(d %% (2:(d-1)) == rep(0,d-2))==TRUE){
print(FALSE)}
else
print(TRUE)
}
}
The problem is when I run my program it says:
[1] TRUE
Error in if (return.prime(i)) { : argument is of length zero
I do not know what causes the length zero.
R doesn't work that way. You're just having the function print the word "TRUE" or "FALSE". Instead, you need to ?return TRUE or FALSE. Consider:
return.prime <- function(d){
if(d==1){ return(FALSE) }
if(d==2){ return(TRUE) }
if(d !=2 && d!=1){
if(any(d %% (2:(d-1)) == rep(0,d-2))==TRUE){
return(FALSE)
} else{
return(TRUE)
}
}
}
i <- 2
j <- 0
for (i in 2:1000) {
if(return.prime(i)){j = j + 1}
i = i + 1
}
j # [1] 168

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

sapply in r with user defined function

My code is as follows:
data("USArrests")
AssignLevel <- function(p,quartiles)
{
if (p < quartiles[1])
rlevel <-"LOW"
else if (p < quartiles[2])
rlevel <-"MODERATE"
else if (p < quartiles[3])
rlevel <-"HIGH"
else level <-"VERY HIGH"
return (rlevel)
}
k<-USArrests$UrbanPop
k
q<- quantile(USArrests$UrbanPop, c(.25,.5,.75))
newCol <- sapply(USArrests$UrbanPop,AssignLevel(k,q))
I'm trying to change the value of every state's urban pop value into one of the corresponding quartiles. It works when I run AssignLevel(k,q) but not when I run in in sapply.
I agree that the cut solution is better. For fun, here is how to resolve your current issue:
data("USArrests")
AssignLevel <- function(p,quartiles) {
if (p < quartiles[[1]]){
rlevel <- "LOW"
} else if(p < quartiles[[2]]) {
rlevel <- "MODERATE"
} else if(p < quartiles[[3]]) {
rlevel <- "HIGH"
} else {
rlevel <- "VERY HIGH"
}
return (rlevel)
}
k <- USArrests$UrbanPop
q <- quantile(k, c(.25,.5,.75))
newCol <- sapply(k,AssignLevel,q)

Resources