vectors
indexing
Author

Thomas Manke

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 Commands

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 values
1/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.

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/

SessionInfo

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 pi
x         # 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 vector
v           # 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 cumbersome
v=1:5             # more efficient

v=seq(from=1, to=5, by=1) # same as before but more flexibilty
v=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-1
v2=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 

Vector Operations

Code
v1=1:3      # = c(1,2,3)
v2=rep(2,3) # = c(2,2,2)

v1 + v2    # elementwise addition
v1 * v2    # ... multiplication
v1 > v2    # ... comparisons
v1 %*% v2  # scalar product

Task: Define your own vectors and explore some functions and operations (sort, rank, …)

Misc: Concepts and Pecularities

Code
v=1:4                    # Vector Definition

v + 10                   # Recycling
v + c(10,20)             # Recycling
v + c(10,20,30)          # Warning != Error

#v %*% c(10,20,30)        # Error = Error

Naming of Vectors

Code
v=1:4
names(v)

letters                  # built-in vector. Try LETTERS 
typeof(letters)          # there is more than numbers

names(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()