3 + 7
[1] 10
9 / 3
[1] 3
9 * (3+2)
[1] 45
12^2 + 1.5*10
[1] 159
10 %/% 3
[1] 3
10 %% 3
[1] 1
Also see help(Syntax)
for operator precendence / associativity: This determines the order in which computations are carried out when multiple operators are present.
Using operators as functions
"/"(1, 10)
[1] 0.1
"+"(2, 3)
[1] 5
sqrt(4)
[1] 2
sin(pi/2)
[1] 1
abs(-4)
[1] 4
log10(100)
[1] 2
exp(1)
[1] 2.718282
round(1.271)
[1] 1
round(pi, digits=3)
[1] 3.142
ceiling(1.2)
[1] 2
floor(3.7)
[1] 3
trunc(22.913)
[1] 22
exp(1)^((0+1i)*pi)
[1] -1+0i
exp(1)^(-pi/2) - (0+1i)^(0+1i)
[1] 0+0i
sqrt(-1)
[1] NaN
sqrt(-1+0i)
[1] 0+1i
$integer.max .Machine
[1] 2147483647
$double.eps .Machine
[1] 2.220446e-16
1/0
[1] Inf
is.infinite(1/0)
[1] TRUE
0/0
[1] NaN
is.nan(0/0)
[1] TRUE
NULL
is.null(NULL)
[1] TRUE
2
x1 <- 10
x2 <- -7
x3 <-* 2 x1
[1] 4
^x1 + x3 x2
[1] 93
TRUE
[1] TRUE
FALSE
[1] FALSE
!TRUE
[1] FALSE
!FALSE
[1] TRUE
isTRUE(TRUE)
[1] TRUE
isTRUE(FALSE)
[1] FALSE
TRUE == TRUE
[1] TRUE
TRUE == FALSE
[1] FALSE
TRUE != TRUE
[1] FALSE
TRUE != FALSE
[1] TRUE
TRUE & TRUE
[1] TRUE
TRUE & FALSE
[1] FALSE
FALSE & FALSE
[1] FALSE
FALSE & TRUE
[1] FALSE
TRUE | TRUE
[1] TRUE
TRUE | FALSE
[1] TRUE
FALSE | FALSE
[1] FALSE
FALSE | TRUE
[1] TRUE
xor(TRUE, FALSE)
[1] TRUE
xor(TRUE, TRUE)
[1] FALSE
Short-circuit logical comparisons with vectors
c(TRUE, FALSE, FALSE) && c(TRUE, TRUE, FALSE)
[1] TRUE
c(FALSE, FALSE, TRUE) || c(FALSE, TRUE, FALSE)
[1] FALSE
4 < 8
[1] TRUE
7 < 3
[1] FALSE
4 > 4
[1] FALSE
4 >= 4
[1] TRUE
TRUE
any(c(FALSE, FALSE, FALSE))
[1] FALSE
any(c(FALSE, FALSE, TRUE))
[1] TRUE
all(c(TRUE, TRUE, FALSE))
[1] FALSE
any(c(TRUE, TRUE, TRUE))
[1] TRUE
In an empty vector, there is no element that is FALSE
, therefore:
all(numeric(0))
[1] TRUE
In an empty vector, you cannot pick an element that is TRUE
, therefore:
any(numeric(0))
[1] FALSE
== 4 4L
[1] TRUE
identical(4L, 4)
[1] FALSE
0.1 + 0.2 == 0.3
[1] FALSE
1 %/% 0.1
[1] 10
sin(pi)
[1] 1.224647e-16
1 - ((1/49) * 49)
[1] 1.110223e-16
1 - ((1/48) * 48)
[1] 0
What every computer scientist should know about floating-point arithmetic
isTRUE(all.equal(0.123450001, 0.123450000))
[1] TRUE
0.123400001 == 0.123400000
[1] FALSE
all.equal(0.12345001, 0.12345000)
[1] "Mean relative difference: 8.100445e-08"
isTRUE(all.equal(0.12345001, 0.12345000))
[1] FALSE
R markdown - markdown - R code - all posts