Skip to contents

Alternative return for empty, null or na statements.Return alternative if the value of expression is empty or NA or NULL

Usage

or(test, alternative)

test %or% alternative

Arguments

test

an object to return

alternative

alternative object to return

Value

value of test if not null or empty, else return value of alternative

Note

Equivalent to Nullish coalescing operator ?? in javascript or PHP like $Var = $operand1 ?? $operand2;

Examples


test1 <- c(4,NA,5,2,0,21)

test2 <- data.frame(ID = 1:10,ED = LETTERS[10:1])

# One may also choose to use

test2[,1] %or% "A"
#>  [1]  1  2  3  4  5  6  7  8  9 10

test2[which(test2$ID == 323),2] %or% "CCCCC"
#> [1] "CCCCC"

number(1) %or% "Placeholder"
#> [1] 803234389

number(10) %or% "Placeholder"
#>  [1]  85229418 934673902 558483207 793261647 651436069  73949913  57500133
#>  [8] 514086690 996776318 332311329

NA %or% "Random"
#> [1] "Random"

NULL %or% "Random"
#> [1] "Random"

"" %or% "Random"
#> [1] "Random"

or(test1[which(test1==4)],100)
#> [1] 4


or(test1[which(test1==43)],100)
#> [1] 100

or(test2[which(test2$ID == 10),2],"BBBBB")
#> [1] "A"

or(test2[which(test2$ID == 323),2],"CCCCC")
#> [1] "CCCCC"