For now, the most important panel is the so-called “Console” with the prompt (“>”).
This is where the first interactions with the R-software will take place.
The other panels are for: output, help and other information.
Task: In the Console try to repeat and understand the following operations
Code
2+2
[1] 4
Code
2^3# This is a comment: try also 2**3
[1] 8
Code
1+2+3/6# beware of precedence rules
[1] 3.5
Code
sqrt(2) # functions
[1] 1.414214
Code
sin(pi/2) # functions and built-in constants
[1] 1
Code
# Special values1/0# R knows infinity! Try 1/Inf and 1/inf
[1] Inf
Code
0/0# Not a number! Try sqrt(-1)
[1] NaN
Command History
All R-commands are tracked in a history. It maybe accessed using the cursors (or the history panel in RStudio). This is extremely useful to navigate to previous commands, repeat them, or change them slightly.
Task: Using the history, determine \(\sqrt 3\).
First help
There are various different ways to get help
Code
?sin
Use help to find out more about: sqrt, exp or anything else of interest.
Every language operates in a certain context and is context-dependent.
Software depends on other software and packages.
It is important to be aware of those (often complex) dependencies.
One way to communicate this context is with the output from sessionInfo().
Code
sessionInfo()
R version 4.2.3 (2023-03-15)
Platform: x86_64-conda-linux-gnu (64-bit)
Running under: Ubuntu 22.04.4 LTS
Matrix products: default
BLAS/LAPACK: /home/runner/micromamba/envs/Rintro/lib/libopenblasp-r0.3.27.so
locale:
[1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
[4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.2.3 fastmap_1.1.1 cli_3.6.2 tools_4.2.3
[5] htmltools_0.5.8.1 yaml_2.3.8 rmarkdown_2.25 knitr_1.46
[9] jsonlite_1.8.8 xfun_0.43 digest_0.6.35 rlang_1.1.3
[13] evaluate_0.23
Query: Using sessionInfo() determine which R-version you are running.
Assigning values to objects
Goal: store results of computation in new variables
Code
x <-2*pi # pre-defined constant pix # see value of object x = show(x)
[1] 6.283185
Code
x =2# "=" as in most other languages. x # x was overwritten
[1] 2
Code
x==3# another logical comparison (x unchanged)
[1] FALSE
Code
x+x # passing objects to functions (+)
[1] 4
Task: look at the following objects and understand the differences
Code
pi hi"hi"
Simple Vectors
Vectors illustrate how complex data structures can be built from smaller blocks. Here we learn how to create and inspect vectors.
Code
v=c(1,2) # combine arguments into vectorv # display v
[1] 1 2
Accessing Vectors
Frequently we need to access specific elements from a vector
Code
v[1]
[1] 1
Task: access the following vector elements: v[1], v[2], v[3], v[1:3], v[-1]
Vector Generation
Code
v=c(1,2,3,4,5) # combining many elements can get cumbersomev=1:5# more efficientv=seq(from=1, to=5, by=1) # same as before but more flexibiltyv=seq(1,5,0.5) # short-hand defaults
Tasks:
Explore the seq() function and inspect other parameter options.
Create a vector with 5 numbers equally spaced from +2 to -1.9
[1] 2.000 1.025 0.050 -0.925 -1.900
Task: Understand the difference between the two vectors v1 and v2
Code
v1=1:10-1v2=1:(10-1)
Functions for vectors
Code
str(v) # structure of v (especially useful for long and structured objects)
num [1:5] 2 1.025 0.05 -0.925 -1.9
Code
typeof(v) # type of v ( ~ storage mode)
[1] "double"
Code
class(v) # class of v (determines how certain functions will work with v)
[1] "numeric"
Code
length(v) # length
[1] 5
Code
rev(v) # reverse vectors
[1] -1.900 -0.925 0.050 1.025 2.000
Code
sum(v)
[1] 0.25
Code
summary(v)
Min. 1st Qu. Median Mean 3rd Qu. Max.
-1.900 -0.925 0.050 0.050 1.025 2.000
v=1:4names(v)letters # built-in vector. Try LETTERS typeof(letters) # there is more than numbersnames(v)=letters[1:4] # naming of vector elements (for convenience)v["b"] # index by name
Learning Curve:
Rstudio: Starting an R project
work with console
simple functions (input, output), logical operations and parameters
functions can behave differently on different input: summary()
see warning messages and errors
getting help: ?sqrt
software dependencies and the importance of sesssionInfo()
Source Code
---title: "01: First Steps"author: "Thomas Manke"categories: - vectors - indexing---# Create R project**Best practice:** Each R project should have their dedicated directory. This directory will hold code, data, results etc.**Task**: Open a new project with "File > New Project ..."Notice the following distinction:- When running Rstudio locally, the project directory will reside on your **local** computer.- When accessing a web server, the project directory will also be on that server.**Task**: Familiarize yourself with the layout and the various panels in Rstudio. **Query**: Utilization: https://pollev.com/thomasmanke101# Console and CommandsFor now, the most important panel is the so-called "Console" with the prompt (">"). This is where the first interactions with the R-software will take place. The other panels are for: output, help and other information.**Task**: In the Console try to repeat and understand the following operations```{r console}2+22^3 # This is a comment: try also 2**31+2+3/6 # beware of precedence rulessqrt(2) # functionssin(pi/2) # functions and built-in constants# Special values1/0 # R knows infinity! Try 1/Inf and 1/inf0/0 # Not a number! Try sqrt(-1)```# Command HistoryAll R-commands are tracked in a *history*. It maybe accessed using the cursors (or the history panel in RStudio). This is extremely useful to navigate to previous commands, repeat them, or change them slightly.**Task**: Using the history, determine $\sqrt 3$.# First helpThere are various different ways to get help```{r help, eval=FALSE}?sin```Use help to find out more about: sqrt, exp or anything else of interest.# Community help* online courses: https://software-carpentry.org/lessons, Coursera, Udacity, ...* bootcamp: https://github.com/jknowles/r_tutorial_ed* QuickR: https://www.statmethods.net * https://stackoverflow.com, https://www.r-bloggers.com/# SessionInfoEvery language operates in a certain context and is context-dependent.Software depends on other software and packages.It is important to be aware of those (often complex) dependencies.One way to communicate this context is with the output from sessionInfo().```{r sessionInfo}sessionInfo()```**Query**: Using sessionInfo() determine which R-version you are running.****# Assigning values to objects**Goal**: store results of computation in new variables```{r simple_obejcts}x <- 2*pi # pre-defined constant pix # see value of object x = show(x) x = 2 # "=" as in most other languages. x # x was overwrittenx==3 # another logical comparison (x unchanged)x+x # passing objects to functions (+)```**Task**: look at the following objects and understand the differences```{r more_obejects, eval=FALSE, echo=TRUE}pi hi"hi"```****# Simple VectorsVectors illustrate how complex data structures can be built from smaller blocks. Here we learn how to create and inspect vectors.```{r vectors}v=c(1,2) # combine arguments into vectorv # display v```# Accessing VectorsFrequently we need to access specific elements from a vector```{r access}v[1]```**Task**: access the following vector elements: v[1], v[2], v[3], v[1:3], v[-1]# Vector Generation```{r vector_generation}v=c(1,2,3,4,5) # combining many elements can get cumbersomev=1:5 # more efficientv=seq(from=1, to=5, by=1) # same as before but more flexibiltyv=seq(1,5,0.5) # short-hand defaults```**Tasks**: - Explore the seq() function and inspect other parameter options. - Create a vector with 5 numbers equally spaced from +2 to -1.9```{r, echo=FALSE}v=seq(2,-1.9,length.out=5)v```**Task**: Understand the difference between the two vectors v1 and v2```{r, eval=FALSE}v1=1:10-1v2=1:(10-1)```# Functions for vectors```{r vectors_funcs}str(v) # structure of v (especially useful for long and structured objects)typeof(v) # type of v ( ~ storage mode)class(v) # class of v (determines how certain functions will work with v)length(v) # lengthrev(v) # reverse vectorssum(v)summary(v)```# Vector Operations```{r vector_ops, eval=FALSE}v1=1:3 # = c(1,2,3)v2=rep(2,3) # = c(2,2,2)v1 + v2 # elementwise additionv1 * v2 # ... multiplicationv1 > v2 # ... comparisonsv1 %*% v2 # scalar product```**Task**: Define your own vectors and explore some functions and operations (sort, rank, ...)# Misc: Concepts and Pecularities```{r misc, eval=FALSE}v=1:4 # Vector Definitionv + 10 # Recyclingv + c(10,20) # Recyclingv + c(10,20,30) # Warning != Error#v %*% c(10,20,30) # Error = Error```# Naming of Vectors```{r naming, eval=FALSE}v=1:4names(v)letters # built-in vector. Try LETTERS typeof(letters) # there is more than numbersnames(v)=letters[1:4] # naming of vector elements (for convenience)v["b"] # index by name```****# Learning Curve:* Rstudio: Starting an R project* work with console * simple functions (input, output), logical operations and parameters* creating simple objects and assigning values* inspecting objects: v, str(v), typeof(v), length(v)* flexible creation of vectors* vector indices and subsetting* vector operations and recycling* special values: Inf, NaN, NA* functions can behave differently on different input: summary()* see warning messages and errors* getting help: ?sqrt* software dependencies and the importance of sesssionInfo()