R command.quit() or q()
in the console.Enter
button.## [1] 6.283185
## [1] 0.5403023
=,
<-, <<-.
<- has the highest priority and it is standard
assign operator in R,<<- is used inside the function to overwrite the
value of a global variable,= is used for passing the parameter values into the
function.## [1] 5
## [1] 2
## [1] 3
## Error in b <- a = 10: could not find function "<-<-"
## [1] 5
## [1] 2
## [1] 10
## [1] 10
## [1] 2
## [1] 2.698830 2.821433 1.984869
source().########################## test_script.R ##############################
# Function
# f <- function(x, y) {
# x <- 2*x
# y <<- 2*y
# }
# Main part of the script
# x <- 2
# y <- 2
#
# print(x)
# print(y)
# x
#
# f(2,2)
#
# cat("x =",x,"\n")
# cat("y =",y,"\n")
########################## Script execution ###############################
source("test_script.R")## [1] 2
## [1] 2
## x = 2
## y = 8
#
character.Source
button.Ctrl+Enter key combination or the Run
button.print() and cat() functions are used
to print variable values to the screen.## y = 3
## [1] 4
R are real numbers.## [1] 10
## [1] "double"
L letter
after the number.## [1] "integer"
## [1] "double"
## [1] "integer"
a+bi.## [1] 2+3i
## [1] "complex"
## Warning in sqrt(-1): NaNs produced
## [1] NaN
## [1] 0+1i
## [1] 2300
NaN (Not a Number) nad
infinities Inf, -Inf## [1] Inf
## [1] 0
## [1] NaN
## [1] "Ala ma kota"
## [1] "Pakiet R"
## [1] "character"
paste() function is used to combine strings.## [1] "I like trains"
character is not
a vector of chars.## [1] 1
TRUE or T) and
false (FALSE or F).## [1] FALSE
## [1] TRUE
## [1] "logical"
## [1] 2
## [1] "double"
NA - Not Available).c() function creates a vector from individual
elements of the same type.## [1] -1 2 5
seq() function or with a simple colon.## [1] 1 2 3 4 5 6 7 8 9 10
## [1] -10 -8 -6 -4 -2 0 2 4 6 8 10
rep() function is used to generate vectors with
repetitions.## [1] TRUE TRUE TRUE TRUE TRUE
## [1] 1 2 3 1 2 3 1 2 3
## [1] 1 1 1 2 2 2 3 3 3
factor() function.levels() function returns the levels.education <- factor (c ("primary", "tertiary", "secondary", "secondary", "tertiary", "secondary"))
education## [1] primary tertiary secondary secondary tertiary secondary
## Levels: primary secondary tertiary
## [1] "primary" "secondary" "tertiary"
## [1] "integer"
## Warning in Ops.factor(education, 1): '+' not meaningful for factors
## [1] NA NA NA NA NA NA
list() function.## $int
## [1] 1 2 3 4 5 6 7 8 9 10
##
## $x
## [1] 2.71
##
## $text
## [1] "a" "b" "c"
##
## $logic
## [1] TRUE TRUE TRUE TRUE TRUE
matrix()
function.## [,1] [,2] [,3]
## [1,] 0 0 0
## [2,] 0 0 0
## [,1] [,2]
## [1,] 1 5
## [2,] 2 6
## [3,] 3 7
## [4,] 4 8
## [,1] [,2]
## [1,] "a" "c"
## [2,] "b" "d"
byrow = TRUE parameter.## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
## [3,] 5 6
## [4,] 7 8
array() function.## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 10 13 16
## [2,] 11 14 17
## [3,] 12 15 18
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 19 22 25
## [2,] 20 23 26
## [3,] 21 24 27
data.frame() function.## numbers logic text
## 1 5 TRUE a
## 2 4 TRUE b
## 3 3 TRUE c
## 4 2 TRUE d
## 5 1 TRUE e
1 (not zero as in C, C++ or Python)
using square brackets [].c() and
seq() functions or a semicolon.## [1] 11 12 13 14 15
## [1] 12 13 14 15 16 17 18 19 20
## [1] 11 12 13 14 18
## [1] 11 13 14 16 17 18 19 20
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
## [1] 1 4 7
## [1] 1 2 3
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
## [,1] [,2]
## [1,] 1 7
## [2,] 3 9
## v1 v2 v3 v4
## 1 0 2 -1
## [1] "v1" "v2" "v3" "v4"
## v1
## 1
## v3
## 2
## col1 col2 col3
## row1 1 4 7
## row2 2 5 8
## row3 3 6 9
## NULL
## col1 col2 col3
## 2 5 8
## row1 row2 row3
## 7 8 9
## [1] 4
$ operator, i.e. list_name$variable_name. It
is worth remembering that a single square bracket returns a sublist
(i.e. the result is a list) and to get the same result as with the
$ operator, you must use a double brackets
[[...]].## [1] 1 2 3 4 5 6 7 8 9 10
## $int
## [1] 1 2 3 4 5 6 7 8 9 10
## [1] 1 2 3 4 5 6 7 8 9 10
## numbers logic text
## 1 5 TRUE a
## 2 4 TRUE b
## 3 3 TRUE c
## 4 2 TRUE d
## 5 1 TRUE e
## numbers logic text
## 1 5 TRUE a
## 2 4 TRUE b
## 3 3 TRUE c
## [1] TRUE TRUE TRUE TRUE TRUE
## [1] 5 4 3 2 1
## [1] 16 17
## [1] "b" "c"
## [1] 5 4 3
## numbers
## 1 5
## 2 4
## 3 3
## numbers
## 1 5
## 2 4
## 3 3
Let define following vectors w, u and
matrices A, B:
## [1] 1 2
## [1] 3 4
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
## [,1] [,2]
## [1,] 4 2
## [2,] 3 1
We can perform the following operations on vectors:
## [1] 4 6
## [1] 6 7
## [1] 2 4
## [,1]
## [1,] 11
## [1] 3 8
Similar operations can be performed on matrices. In addition, we can use another, very useful functions:
## [,1] [,2]
## [1,] 5 5
## [2,] 5 5
## [,1] [,2]
## [1,] 2 4
## [2,] 3 5
## [,1] [,2]
## [1,] 2 6
## [2,] 4 8
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
## [1] -2
## [,1] [,2]
## [1,] 13 5
## [2,] 20 8
## eigen() decomposition
## $values
## [1] 5.3722813 -0.3722813
##
## $vectors
## [,1] [,2]
## [1,] -0.5657675 -0.9093767
## [2,] -0.8245648 0.4159736
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 25
The length of the condition has to be one, otherwise the conditional statement will not be executed.
## Error in if (x%%3) {: the condition has length > 1
The conditions with length greater than one can be used in
ifelse() function, which check the condition for each
element of a given vector.
## [1] "It is not divisible by 3" "It is not divisible by 3"
## [3] "It is divisible by 3" "It is not divisible by 3"
## [5] "It is not divisible by 3" "It is divisible by 3"
## [7] "It is not divisible by 3" "It is not divisible by 3"
## [9] "It is divisible by 3" "It is not divisible by 3"
The following functions are very useful when processing data in vector format:
## [1] 5
## [1] -0.2
## [1] 3.114482
## [1] -5 3 0 -1 2
## [1] -1
## [1] 2 1 1 4 -1
## [1] 0
## [1] 2 -2 0 0 0
## [1] -5
## [1] 5
## [1] 3
## [1] 4
## [1] -5 -1 0 2 3
## [1] 3 2 0 -1 -5
## $x
## [1] -5 -1 0 2 3
##
## $ix
## [1] 5 2 3 1 4
NA (Not Available) elements until the parameter
na.rm=TRUE is used.## [1] NA
## [1] NA
## [1] 15
## [1] 3.75
which function returns the indexes of elements that
meet a given condition.## [1] 4 5
## [1] 3
## integer(0)
which(y == NA) will not give the
expected result. To find the indexes of NA,
NaN or Inf elements, use the
is.na, is.nan, is.finite and
is.infinite functions inside the which
function .## [1] NaN NA Inf -Inf 10 15
## [1] TRUE TRUE FALSE FALSE FALSE FALSE
## [1] TRUE FALSE FALSE FALSE FALSE FALSE
## [1] FALSE FALSE TRUE TRUE FALSE FALSE
## [1] 1 2
## [1] 1
## [1] 3 4
Some of the above functions can also be applied to matrices, but in
the case of which.min() and which.max() it is
necessary to use the arrayInd() function additionally to
determine the matrix indexes - otherwise we will only get the vector
index.
## [,1] [,2] [,3] [,4]
## [1,] 1 5 9 13
## [2,] 2 6 10 14
## [3,] 3 7 11 15
## [4,] 4 8 12 16
## [1] 16
## [1] 136
## [1] 8.5
## [1] 4.760952
## [1] 1
## [1] 16
## [1] 1
## [1] 16
## [,1] [,2]
## [1,] 1 1
## [,1] [,2]
## [1,] 4 4
As in any programming language, a very important element of the R is creating your own functions. The syntax for creating a function is as follows.
The function parameters (arguments) can be any data type or data structure, e.g. vectors, matrices or lists.
multiplication_table <- function (range1, range2) {
return (range1 %o% range2)
}
multiplication_table (1:10, 1:10)## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 2 3 4 5 6 7 8 9 10
## [2,] 2 4 6 8 10 12 14 16 18 20
## [3,] 3 6 9 12 15 18 21 24 27 30
## [4,] 4 8 12 16 20 24 28 32 36 40
## [5,] 5 10 15 20 25 30 35 40 45 50
## [6,] 6 12 18 24 30 36 42 48 54 60
## [7,] 7 14 21 28 35 42 49 56 63 70
## [8,] 8 16 24 32 40 48 56 64 72 80
## [9,] 9 18 27 36 45 54 63 72 81 90
## [10,] 10 20 30 40 50 60 70 80 90 100
The return() instruction is not obligatory - the value
of the function is the value specified in its last line. The dot
character . in R does not have any special role. Therefore
it can be used in the names of variables and functions.
## [1] 7
All values passed to the function are visible and changed locally. If
you need to globally change the value of a variable, use the assignment
operator <<-.
## [1] 2
## [1] 4
A popular form of assistance in data analysis in R is the so-called cheat sheets. They can be found at https://www.rstudio.com/resources/cheatsheets/. Another way is to open the Help menu in RStudio and select Cheat Sheets from the drop-down list.