Today, we will talk about various elements of a programming language and see how they are realized in R.
7 + 9a <- 7ab <- 9bc <- a + bc
## [1] 16## [1] 7## [1] 9## [1] 16
We are not constrained to numbers...
text1 <- 'a'text2 <- 'qwerty'text1 text2
## [1] "a"## [1] "qwerty"
<-
equivalent to =
? Which one shall I use?val <- 3
, val = 3
and 3 -> val
are three equivalent ways of assigning in R, But, it's best to use only <-
to avoid possible confusion. The equal sign =
should be used when setting function arguments ie; f(a = 3)
.A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number.
A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number.
Names such as ".2way" are not valid, and neither are the so-called reserved words.
A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number.
Names such as ".2way" are not valid, and neither are the so-called reserved words.
if, else, repeat, while, function, for, in, next, break, TRUE, FALSE, NULL, Inf, NaN, NA, NA_integer_, NA_real_, NA_complex_, NA_character_
and you also cannot use: c, q, t, C, D, I
and you should not use: T, F
So, how to name variables?
make them informative, e.g. genotypes
instead of fsjht45jkhsdf4
,
So, how to name variables?
make them informative, e.g. genotypes
instead of fsjht45jkhsdf4
,
use consistent notation across your code – the same naming convention,
So, how to name variables?
make them informative, e.g. genotypes
instead of fsjht45jkhsdf4
,
use consistent notation across your code – the same naming convention,
camelNotation vs. dot.notation vs. dash_notation,
So, how to name variables?
make them informative, e.g. genotypes
instead of fsjht45jkhsdf4
,
use consistent notation across your code – the same naming convention,
camelNotation vs. dot.notation vs. dash_notation,
do not give_them_too_long_names
,
So, how to name variables?
make them informative, e.g. genotypes
instead of fsjht45jkhsdf4
,
use consistent notation across your code – the same naming convention,
camelNotation vs. dot.notation vs. dash_notation,
do not give_them_too_long_names
,
in the dot notation avoid my.variable.2
, use my.variable2
instead,
So, how to name variables?
make them informative, e.g. genotypes
instead of fsjht45jkhsdf4
,
use consistent notation across your code – the same naming convention,
camelNotation vs. dot.notation vs. dash_notation,
do not give_them_too_long_names
,
in the dot notation avoid my.variable.2
, use my.variable2
instead,
there are certain customary names:
tmp
- for temporary variables;cnt
for counters;i,j,k
within loops,pwd
- for password...A numeric stores numbers of different types:
x <- 41.99 # assign 41.99 to xtypeof(x)
## [1] "double"
class
what type of object is it for R,typeof()
what R thinks it is,mode()
how S language would see it (backward compatibility),storage.mode()
how is it stored in the memory; useful when talking to C or Java,x <- 1:3class(x)typeof(x)mode(x)storage.mode(x)
## [1] "integer"## [1] "integer"## [1] "numeric"## [1] "integer"
By default, any numeric is stored as double !
y=12 # now assign an integer value to yclass(y) # still numerictypeof(y) # an integer, but still a double!
## [1] "numeric"## [1] "double"
But we can explicitly cast it to integer:
x <- as.integer(x) # type conversion, castingtypeof(x)class(x)is.integer(x)
## [1] "integer"## [1] "integer"## [1] TRUE
We need casting because sometimes a function requires data of some type!
Be careful when casting!
pi <- 3.1415926536 # assign approximation of pi to pipipi <- as.integer(pi) # not-so-careful castingpipi <- as.double(pi) # trying to rescue the situationpi
## [1] 3.141593## [1] 3## [1] 3
Casting is not rounding!
as.integer(3.14)as.integer(3.51)
## [1] 3## [1] 3
floor(3.51) # floor of 3.51ceiling(3.51) # ceiling of 3.51round(3.51, digits=1) # round to one decimal
## [1] 3## [1] 4## [1] 3.5
as.numeric('4.5678')as.double('4.5678')as.numeric('R course is cool!')
## [1] 4.5678## [1] 4.5678## [1] NA
-1/0 # Minus infinity1/0 # Infinity
## [1] -Inf## [1] Inf
and also:
112345^67890 # Also infinity for R1/2e78996543 # Zero for RInf - Inf # Not a Number
## [1] Inf## [1] 0## [1] NaN
Core R supports complex numbers.
z = 7 + 4i # create a complex numberzclass(z)typeof(z)is.complex(z)
## [1] 7+4i## [1] "complex"## [1] "complex"## [1] TRUE
sqrt(-1) # not treated as cplx numbersqrt(-1 + 0i) # now a proper cplx numbersqrt(as.complex(-1)) # an alternative way
## [1] NaN## [1] 0+1i## [1] 0+1i
a <- 7 > 2b <- 2 >= 7abclass(a)typeof(a)
## [1] TRUE## [1] FALSE## [1] "logical"## [1] "logical"
R has three logical values: TRUE, FALSE and NA.
x <- c(TRUE, FALSE, NA)names(x) <- as.character(x)and_truth_table <- outer(x, x, "&") # AND table
TRUE | FALSE | NA | |
---|---|---|---|
TRUE | TRUE | FALSE | NA |
FALSE | FALSE | FALSE | FALSE |
NA | NA | FALSE | NA |
x <- TRUExx <- T # also validxis.logical(x)typeof(x)
## [1] TRUE## [1] TRUE## [1] TRUE## [1] "logical"
x <- TRUEy <- FALSEx + y2 * xx * y
## [1] 1## [1] 2## [1] 0
Never ever use variable names as T or F. Why?
F <- TTF
## [1] TRUE## [1] TRUE
Maybe applicable in politics, but not really in science...
It is easy to work with characters and strings:
character <- 'c'text <- 'This is my first sentence in R.'textcharacterclass(character)typeof(text) # also of 'character' type
## [1] "This is my first sentence in R."## [1] "c"## [1] "character"## [1] "character"
number <- 3.14number.text <- as.character(number) # cast to charnumber.textclass(number.text)as.numeric(number.text) # and the other way round
## [1] "3.14"## [1] "character"## [1] 3.14
text1 <- "John had a yellow " text2 <- "submarine" result <- paste(text1, text2, ".", sep='') result sub("submarine", "cab", result) substr(result, start=1, stop=5)
## [1] "John had a yellow submarine."## [1] "John had a yellow cab."## [1] "John "
txt <- "blue" (txt <- 'red') val <- 345.78 sprintf("The weight of a %s ball is %g g", txt, val)
## [1] "red"## [1] "The weight of a red ball is 345.78 g"
Graphics from
Created: 27-Sep-2021 • Roy Francis • SciLifeLab • NBIS
Today, we will talk about various elements of a programming language and see how they are realized in R.
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |