Using if and stop doesnt work the same as stopifnot - r

I've been working on this school project...
We have a very specific task. If the conditions arent true, I need to stop the code and print a custom message.
First I did this:
This one works, but I can't add a custom warning message I have to include.
alpha <- function(pstar, p){
stopifnot(is.numeric(pstar), length(pstar) == 1, is.numeric(p), is.vector(p), p > 0)
n <- length(p)
((sqrt((sum((pstar - p[1:n])^2)) / n)) / pstar) * 100
}
I thought I´d use if and stop instead.
alpha <- function(pstar, p){
if (!(is.numeric(pstar) && length(pstar) == 1 && is.numeric(p) && is.vector(p) && p > 0))
return(stop("Bad input"))
n <- length(p)
((sqrt((sum((pstar - p[1:n])^2)) / n)) / pstar) * 100
}
But I always get this warning message back:
Warning message:
In is.numeric(pstar) && length(pstar) == 1 && is.numeric(p) && is.vector(p) && :
'length(x) = 5 > 1' in coercion to 'logical(1)'
I can't find where the problem is.
Thank you for helping!

stopifnot() allows a message to be attached to each of the tests, for example the first two tests could be messaged like:
stopifnot("Pstar must be numerical" = is.numeric(pstar),
"pstar must be of length 1" = length(pstar) == 1,
is.numeric(p), is.vector(p), p > 0)
> alpha("a", 20)
Error in alpha("a", 20) : Pstar must be numerical
> alpha(c(2,2), 20)
Error in alpha(c(2, 2), 20) : pstar must be of length 1 > alpha("a", 20)
Does this solve your problem?
Incidentally, your second function appears to work:
alpha("a", 20)
Error in alpha("a", 20) : Bad input
alpha(c(2,2), 20)
Error in alpha(c(2, 2), 20) : Bad input
alpha(2, 1)
[1] 50

Related

Error in if (dataclass == "data.frame") { : the condition has length > 1 [duplicate]

Using R 4.1.3 I observe:
var <- 0
> if(is.data.frame(var) || is.vector(var)) var <- as.matrix(var)
> is.null(var) || (!is.matrix(var) && var == 0) || (dim(var)==c(1,1) && var[1,1]==0)
[1] TRUE
However, with R 4.2.1 I observe the warning on this same code
> var <- 0
> if(is.data.frame(var) || is.vector(var)) var <- as.matrix(var)
> is.null(var) || (!is.matrix(var) && var == 0) || (dim(var)==c(1,1) && var[1,1]==0)
[1] TRUE
Warning message:
In dim(var) == c(1, 1) && var[1, 1] == 0 :
'length(x) = 2 > 1' in coercion to 'logical(1)'
Having a hard time finding the root cause here. Any thoughts on a reason this occurs now and a good fix?
dim(var) == c(1,1) gives two TRUE. See R News below. Possible replacements are
identical(dim(var), c(1L, 1L))
all(dim(var) == c(1,1))
Previously we were told that && and || are safe to use in if () as they silently return a single TRUE or FALSE. But now it will warn you. This actually makes it easier to detect bugs, so I am happy with this change.
Unfortunately, this change has affected a few R packages (see for example: Package ceases to work due to "if" error: condition has length > 1). As a result, the code that used to work smoothly suddenly throws warnings or even errors.

Error in if (tolower(e) %in% c(".tiff", ".tif")) { : the condition has length > 1 [duplicate]

Using R 4.1.3 I observe:
var <- 0
> if(is.data.frame(var) || is.vector(var)) var <- as.matrix(var)
> is.null(var) || (!is.matrix(var) && var == 0) || (dim(var)==c(1,1) && var[1,1]==0)
[1] TRUE
However, with R 4.2.1 I observe the warning on this same code
> var <- 0
> if(is.data.frame(var) || is.vector(var)) var <- as.matrix(var)
> is.null(var) || (!is.matrix(var) && var == 0) || (dim(var)==c(1,1) && var[1,1]==0)
[1] TRUE
Warning message:
In dim(var) == c(1, 1) && var[1, 1] == 0 :
'length(x) = 2 > 1' in coercion to 'logical(1)'
Having a hard time finding the root cause here. Any thoughts on a reason this occurs now and a good fix?
dim(var) == c(1,1) gives two TRUE. See R News below. Possible replacements are
identical(dim(var), c(1L, 1L))
all(dim(var) == c(1,1))
Previously we were told that && and || are safe to use in if () as they silently return a single TRUE or FALSE. But now it will warn you. This actually makes it easier to detect bugs, so I am happy with this change.
Unfortunately, this change has affected a few R packages (see for example: Package ceases to work due to "if" error: condition has length > 1). As a result, the code that used to work smoothly suddenly throws warnings or even errors.

Average Sample Number using Page's CUSUM procedure?

I am trying reproduce the Table 1 results from the page 12 using the equation given the page 13.To access the journal article please click https://arxiv.org/pdf/math/0605322.pdf. The corresponding equation is given below.
My r code is give below. Am I programmed correctly?
mytest=function(n,s,c1){
t = sum(s)
k=which.max(s[19:n]>=c1)
if(k==1 && s[19]<c1)
return(c(n,0))
else
return(c(k,1))
}
for (n in c(100,200,400)){
for (i in c(-0.5, -1.0)){
a1=0
c1 = 20
asn1=0
for (m in 1:1000){
g=c(dnorm(n,0,1))
f=c(dnorm(n,i,1))
s = log(g/f)
test=mytest(n,s,c1)
a1=a1+test[2]
asn1=asn1+test[1]
}
}
out <- list(power= a1/m, asn=asn1/m)
return(out)
}
But I am getting the following errors.
Error in if (k == 1 && s[19] < c1) return(c(n, 0)) else return(c(k, 1)) :
missing value where TRUE/FALSE needed
The first time you call mytest, you have n=100, i=-0.5 which yields s=NaN. Therefore, you get an error on line if(k==1 && s[19]<c1) given that s[19]=NaN.
Here's a workaround, but you need to make sure it does what you expect/wish :
mytest=function(n,s,c1){
if(is.na(s)) return(c(c1,1)) # skips if NaN
t = sum(s)
k=which.max(s[19:n]>=c1)
if(k==1 && s[19]<c1)
return(c(n,0))
else
return(c(k,1))
}

Error in if (num < 0) { : missing value where TRUE/FALSE needed

y <- as.integer(readline(prompt ="Enter a number: "))
factorial = 1
if (y< 0){
print("Error")
} else if (y== 0)
{
print("1")
} else
{
for(i in 1:y) {
factorial = factorial * i
}
return(factorial)
}
wondering why this is giving:
Error in if (y< 0) { : missing value where TRUE/FALSE needed
is it cause the first line has data type NA_integer?
There are three possible ways to pass values to the if statement.
y <- 1
if (y > 0) print("more")
This one works as expected.
y <- 1:3
if (y > 0) print("ignores all but 1st element")
As the warning message will tell you, only the first element was used to evaluate it. You could use any or all to make this right.
y <- NA
if (y > 0) print("your error")
This case actually gives you your error. I would wager a bet that y is somehow NA. You will probably need to provide a reproducible example (with data and the whole shebang) if you'll want more assistance. Note also that it helps visually structure your code to improve readability.

r error occurs in statement with else but not in if statement

My data set contains numeric values with decimals
When I write a simple if statement like this:
data$Segment <- ifelse(data$RankAll> 10,'Gekko',0)
I do not get an error but when I write it like this:
data$Segment <- if (data$RankAll<4) 'Moore' else if
(data$RankAll<6) 'Marvin' else if
(data$RankAll<8)'Branes' else if
(data$RankAll<10) 'Fox' else 'Gekko'
It returns the error:
Warning messages:
1: In if (data$RankAll < 4) "Moore" else if (data$RankAll < 6) "Marvin" else if (data$RankAll < :
the condition has length > 1 and only the first element will be used
2: In if (data$RankAll < 6) "Marvin" else if (data$RankAll < 8) "Branes" else if (data$RankAll < :
the condition has length > 1 and only the first element will be used
3: In if (data$RankAll < 8) "Branes" else if (data$RankAll < 10) "Fox" else "Gekko" :
the condition has length > 1 and only the first element will be used
4: In if (data$RankAll < 10) "Fox" else "Gekko" :
the condition has length > 1 and only the first element will be used
What am I doing wrong?
See SamDickson's approach in the comment above for an approach that keeps the same logic as the original.
To do the same thing with DRYer code:
data$Segment <- cut(data$RankAll, breaks = c(0, 4, 6, 8, 10, 100), right = FALSE)
levels(data$Segment) <- c('Moore', 'Marvin', 'Branes', 'Fox', 'Gekko')

Resources