<
1.1 Type anything in the dialog box and R will calculate it and output the answer. Let’s try some simple math. Type the following command.
1 + 1
## [1] 2
[1] 2 Here is your result, 2. It is printed on the console immediately after your entry.
1.1 Type the string “Arr, matey!” (Don’t forget the quotation marks!).
"Arr, matey!"
## [1] "Arr, matey!"
Now try multiplying 6 times 7 (* is the multiplication operator).
6 * 7
## [1] 42
1.2 Some expressions return a “Boolean value”: TRUE or FALSE. (Many programming languages refer to these as “boolean” values.) Let’s try typing an expression that gives us a Boolean value:
3 < 4
## [1] TRUE
And another Boolean value (note that you need a double equal sign to check if two values are equal - a single equal sign will not work):
2 + 2 == 5
## [1] FALSE
T and F are abbreviations for TRUE and FALSE. Try this:
T == TRUE
## [1] TRUE
1.3 As in other programming languages, you can store values in a variable to access them later. Type x <- 42 to store a value in x.
x <- 42
x can now be used in expressions in place of the original result. Try dividing x by 2 (/ is the divide operator).
x / 2
## [1] 21
You can reassign any value to a variable at any time. Try assigning “Arr, matey!” to x.
x <- "Arr, matey!"
You can print the value of a variable at any time by simply typing its name in the console. Try printing the current value of x.
x
## [1] "Arr, matey!"
Now try assigning the logical value TRUE to x.
x <- TRUE
1.4 You call a function by typing its name followed by one or more arguments in parentheses. Let’s try using the sum function to add up several numbers. Enter:
sum(1, 3, 5)
## [1] 9
Some arguments have names. For example, to repeat a value 3 times, you would call the rep function and give it the times argument:
rep("Yo ho!", times = 3)
## [1] "Yo ho!" "Yo ho!" "Yo ho!"
Try calling the sqrt function to get the square root of 16.
sqrt(16)
## [1] 4
help(functionname) displays help for the specified function. Try displaying help for the sum function:
help(sum)
## starting httpd help server ... done
suma pakiet:base Dokumentacja R
Suma elementów wektora
Opis:
'sum' zwraca sumę wszystkich wartości występujących w jej argumentach.
Użycie:
sum(..., na.rm = FALSE)
… (Don’t worry about that optional argument to.rm, we’ll deal with that later). example(functionname) displays examples of how to use the specified function. Try displaying examples for the min function:
example(min)
##
## min> require(stats); require(graphics)
##
## min> min(5:1, pi) #-> one number
## [1] 1
##
## min> pmin(5:1, pi) #-> 5 numbers
## [1] 3.141593 3.141593 3.000000 2.000000 1.000000
##
## min> x <- sort(rnorm(100)); cH <- 1.35
##
## min> pmin(cH, quantile(x)) # no names
## [1] -2.18085241 -0.71792818 0.01370801 0.67766938 1.35000000
##
## min> pmin(quantile(x), cH) # has names
## 0% 25% 50% 75% 100%
## -2.18085241 -0.71792818 0.01370801 0.67766938 1.35000000
##
## min> plot(x, pmin(cH, pmax(-cH, x)), type = "b", main = "Huber's function")
##
## min> cut01 <- function(x) pmax(pmin(x, 1), 0)
##
## min> curve( x^2 - 1/4, -1.4, 1.5, col = 2)
##
## min> curve(cut01(x^2 - 1/4), col = "blue", add = TRUE, n = 500)
##
## min> ## pmax(), pmin() preserve attributes of *first* argument
## min> D <- diag(x = (3:1)/4) ; n0 <- numeric()
##
## min> stopifnot(identical(D, cut01(D) ),
## min+ identical(n0, cut01(n0)),
## min+ identical(n0, cut01(NULL)),
## min+ identical(n0, pmax(3:1, n0, 2)),
## min+ identical(n0, pmax(n0, 4)))
Now try calling help for the rep function:
help(rep) rep package:base R documentation
Replicate Elements of Vectors and Lists
Description:
'rep' replicates the values in 'x'. This is a generic function, and the
(internal) default method is described here.
…
Typing commands every time you need them only works for short scripts, of course. R commands can also be saved in text files (with the “.R” extension, by convention) for execution later. They can be run directly from the command line or from a running instance of R.
Let’s make an object a<-“This is information” and then save it in the current directory under the name File1.r
a<-"To jest informacja"
save(a, file ="Plik1.r")
We can now see if File1.r exists in the current floder.
list.files()
## [1] "Cw_1.docx" "Cw_1.html"
## [3] "Cw_1.Rmd" "Cw_1_EN.html"
## [5] "Cw_1_EN.Rmd" "Cw_1_EN_files"
## [7] "Cw_1n.nb.html" "Cw_1n.Rmd"
## [9] "Cw_3.html" "Cw_3.Rmd"
## [11] "Cw_4.html" "Cw_4.Rmd"
## [13] "Cw_4_EN.docx" "Cw_4_EN.html"
## [15] "Cw_4_EN.pdf" "Cw_4_EN.Rmd"
## [17] "Cw_6_EN_AN.html" "Cw_6_EN_AN.Rmd"
## [19] "Cw_6_PL_AN.html" "Cw_6_PL_AN.Rmd"
## [21] "Cw_8_EN_PR_GC.html" "Cw_8_EN_PR_GC.nb.html"
## [23] "Cw_8_EN_PR_GC.Rmd" "Cw_8_PL_PR_GC.html"
## [25] "Cw_8_PL_PR_GC.Rmd" "Cw1.Rhistory"
## [27] "Cw2" "Cw2-EN-RynekBankowy.html"
## [29] "Cw2-EN-RynekBankowy.pdf" "Cw2-EN-RynekBankowy.Rmd"
## [31] "Cw2-RynekBankowy.html" "Cw2-RynekBankowy.pdf"
## [33] "Cw2 RynekBankowy.Rmd" "Cw2_EN_RynekBankowy.html"
## [35] "Cw2_EN_RynekBankowy.pdf" "Cw2_EN_RynekBankowy.Rmd"
## [37] "Cw3" "Cw3_EN.html"
## [39] "Cw3_EN.Rmd" "Cw3_PL.html"
## [41] "Cw3_PL.Rmd" "Cw4 AuditMatieraltiy"
## [43] "Kody pomocnicze" "OB"
## [45] "path" "pentle.R"
## [47] "Plik1.r" "Plik2.r"
## [49] "Plik3.r" "R Audit.Rproj"
## [51] "references.bib" "rmarkdown.pdf"
## [53] "rsconnect" "test.html"
## [55] "test.Rmd" "Z1_A_MQT_Probkowanie.Rmd"
## [57] "Z1_MQT_Probkowanie.docx" "Z1_MQT_Probkowanie.html"
## [59] "Z1_MQT_Probkowanie.Rmd"
R has a self-study system called Swirl [https://swirlstats.com/]. It is package with built-in learning courses, to use it you need to:
First, install the swirl package:
install.packages(“swirl”) from the console.
Then run it
library(swirl)
##
## | Hi! I see that you have some variables saved in your workspace. To keep
## | things running smoothly, I recommend you clean up before starting swirl.
##
## | Type ls() to see a list of the variables in your workspace. Then, type
## | rm(list=ls()) to clear your workspace.
##
## | Type swirl() when you are ready to begin.
And from the console give the swirl() command to start the learning process. Mastering this package requires 2-3 weeks of work, but it’s worth it.