Basic arithmetic and logic

TODO

  • link to vector

R as a pocket calculator

Arithmetic operators

See help(Arithmetic)

[1] 10
[1] 3
[1] 159
[1] 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. Use parentheses to make execution order explicit.

[1] 45
[1] 29

Using operators in function notation

[1] 0.1
[1] 5

Standard math functions

[1] 2
[1] 1
[1] 4
[1] 2
[1] 2.718282

Rounding numbers

[1] 1
[1] 3.142
[1] 2
[1] 3
[1] 22

Complex numbers

[1] -1+0i
[1] 0+0i
[1] NaN
[1] 0+1i

Special values

[1] 2147483647
[1] 2.220446e-16
[1] Inf
[1] TRUE
[1] NaN
[1] TRUE
NULL
[1] TRUE

Using variables (objects)

[1] 4
[1] 93

Logic

Logical values

[1] TRUE
[1] FALSE
[1] FALSE
[1] TRUE
[1] TRUE
[1] FALSE

Logical comparisons

[1] TRUE
[1] FALSE
[1] FALSE
[1] TRUE
[1] TRUE
[1] FALSE
[1] FALSE
[1] FALSE
[1] TRUE
[1] TRUE
[1] FALSE
[1] TRUE
[1] TRUE
[1] FALSE

Short-circuit logical comparisons with vectors

[1] TRUE
[1] FALSE

Arithmetic comparisons

[1] TRUE
[1] FALSE
[1] FALSE
[1] TRUE

Checking whether any or all elements are TRUE

[1] FALSE
[1] TRUE
[1] FALSE
[1] TRUE

In an empty vector, there is no element that is FALSE, therefore:

[1] TRUE

In an empty vector, you cannot pick an element that is TRUE, therefore:

[1] FALSE

Numeric representations

Integers vs. decimal numbers

[1] TRUE
[1] FALSE

Floating point arithmetic

[1] FALSE
[1] 9
[1] 1.224647e-16
[1] 1.110223e-16
[1] 0

What every computer scientist should know about floating-point arithmetic

Checking decimal numbers for equality

[1] TRUE
[1] FALSE
[1] "Mean relative difference: 8.100445e-08"
[1] FALSE

Get the article source from GitHub

R markdown - markdown - R code - all posts