R
“The R Project for Statistical Computing”
http://www.r-project.org/
1 / 16
R – introduction
the R software environment
• implements the programming language S (often1 referred to as R)
• free to use and Jopen source
• widely adopted in statistics and data mining
• comes with a large amount of libraries (mostly free, too)
• libaries usually implemented in a mix of C and R
the S language
• dynamically typed
– types need not be specified
– autoconversion can hide programming errors
• is a functional programming language
– usually: no side effects, functions in the mathematical sense
– here: functions can be used as data
– functions as parameters, return values etc.
1
that includes here, from the next slide on
2 / 16
R – getting help
•
•
•
•
•
•
•
starting point www.r-project.org
web search is complicated, using capital R in search term helps
specialized search engine: rseek.org
consider the R manual cran.r-project.org/manuals.html
good books?
advice on plotting by example: gallery.r-enthusiasts.com/
advice on many specific questions:
stackoverflow.com (use the R-tag)
• for specific functions use ?function to view manual from console
• libraries usually come with these help texts as pdf
3 / 16
using R – interfaces
basic idea: quickly implement procedures in a statistical context
• two types of usage:
– typing commands directly into console
– run scripts
• support by programming environments:
– default: console window (direct input or copy from editor)
– environments provide editing/execution/integrated help view
• development environments (examples, all OS’s, all free)
– RStudio (www.rstudio.com)
– ESS (emacs speaks statistics)
4 / 16
some first steps
> a<-3
> a*2
[1] 6
> b<-aˆ2
> ls()
[1] "a" "b"
• the arrow <- is an assignment (i.e. assign value on the right to
variable on the left)
• ls() lists variables and functions
• the [1] in output is the index (changes for lists/arrays,. . . )
e.g. print list 1, . . . , 36
>1:36
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
[21] 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
5 / 16
the assignment discussion
assign value to variable: assign("a",3);
note that ”a” is a string here
alternatives/shorthands:
– a<-3
– a=3
– 3->a
reasons to not use “=” for assignment:
– confusion with test for equality (“==”)
– parameter assignments in functions: runif(3,min=10,max=23)
– convention (many R programmers are used to the arrow
6 / 16
example for assignment differences:
> runif(3,min=10,max=23)
[1] 15.46222 22.84359 18.26251
> ls()
character(0)
> runif(3,min<-10,max<-23)
[1] 14.25016 13.96283 20.36843
> ls()
[1] "max" "min"
• runif(n,min,max) draws n uniform distributed numbers from
[min, max ]
• first call uses named parameters e.g. runif(3,max=23,min=10)
is equivalent
• second call assigns values to min and max, the result is inserted as
parameter for runif()
avoid that unless you know what you do
7 / 16
vectors
• create with
– rep("a",4) (repeat a 4 times)
– seq(1,4,by=0.7) (sequence start 1, step width 0.7, stop at 4)
– 1:n (list of numbers 1, 2, . . . , n)
– c(1:3,3:1) (concatenate two vectors)
• operations are usually applied element wise
> sqrt(1:4)
[1] 1.000000 1.414214 1.732051 2.000000
> c(1,3,5)+0.1
[1] 1.1 3.1 5.1
this applies for the combination of vectors, too
> (1:3)+(1:3)
[1] 2 4 6
note: R will try to reuse shorter argument if vectors differ in length
8 / 16
more on vectors:
which() return indices that are true
> list=1:5
> list>2
[1] FALSE FALSE TRUE TRUE TRUE
> which(list>2)
[1] 3 4 5
logical vectors can also be used to select parts and reassign them:
> list[c(3,5)]
[1] 3 5
> list[list>2]<-1
> list
[1] 1 2 1 1 1
access length with length()
> length(list)
[1] 5
9 / 16
more on vectors:
[ ] selects a slice of a vector (can be a single element)
[[ ]] selects concrete element
names() accesses names of vector elements (read and write)
#create, name and print list (# is for comments)
> l<-1:3; names(l)<-c("a","b","c"); l
a b c
1 2 3
> l[2] # select slice with single element
b
2
> l[[2]] # select element at position
[1] 2
10 / 16
all elements in a vector are of the same type:
> l[4]="c"; l
a b c
"1" "2" "3" "c"
adding a string element forced conversion to strings
11 / 16
more flexibility with lists
• different types of elements in the same list (including lists)
• access by name
> li=list(index=1, name="first", data=1:5)#create
> li["name"] # select slice (list of names possible)
$name
[1] "first"
> li$name # select single element as in li[["name"]]
[1] "first"
selection by boolean vector is still possible:
> li[!sapply(li, is.numeric)] # ! is boolean not
$name
[1] "first"
sapply(): simply apply function is.numeric to list li
12 / 16
applying functions to lists
create a list with two different versions:
#
>
>
>
>
using for loop:
l=c(); # empty list
for(i in 1:5) {
l=c(l,sqrt(i));#append
}
>l=sapply(1:5,sqrt)
#result (in both cases):
>l
[1] 1.000000 1.414214
[3] 1.732051 2.000000
[5] 2.236068
• left: standard for-loop, right: same result using sapply
• sapply
–
–
–
–
takes a list (e.g. 1:5)
applies a function sqrt() to each entry
sqrt is a function used as argument
collects result in list
13 / 16
functions are values
example: create a variable a with function as value:
> a=sqrt
call that function, execution is triggered by ():
> a(3)
[1] 1.732051
since they are values, they can be printed:
> ls
function (name, pos = -1, envir =
as.environment(pos), all.names = FALSE, pattern) {
if (!missing(name)) {...
ls() in contrast executes the function:
> ls()
[1] "a" "i" "l" "list"
14 / 16
the more general case of apply
sapply() is a simplification of lapply(), compare:
> l=1:2;
>names(l)=c("a","b");
> lapply(l, sqrt)
$a
[1] 1
$b
[1] 1.414214
> l=1:2;
>names(l)=c("a","b");
> sapply(l,sqrt)
a
b
1.000000 1.414214
• lapply() puts every result in a list, sapply() tries to simplify to
vector
15 / 16
flattening lists
• take all the values in a list of lists (of lists)
• create a single list of values
unlist()
• recursive action, when recursive = TRUE
16 / 16
© Copyright 2026 Paperzz