Reputation: 659
When I execute 0 / 0
in R, I would get a NaN
as an output. But is there any way I could print 0 / 0
as 1
? I know I could use some if
statements to achieve it. I would like to know if there is any other way to achieve this.
Upvotes: 1
Views: 994
Reputation: 12074
You could define your own division symbol especially for this. For example,
'|' <- function(a,b)ifelse(a==0 & b==0, 1, a/b)
> 0|0
[1] 1
> 3|4
[1] 0.75
Upvotes: 4
Reputation: 2874
Other way than using If
statement would be
# Assign NaN to vector
a <- 0 / 0
# If is NaN assign value 1
a[is.nan(a)] <- 1
Upvotes: 0