18
Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

Embed Size (px)

Citation preview

Page 1: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

Data, graphics, and programming in R

28.1, 30.1, 2-4.2

Daily:10:00-12:45 & 13:45-16:30EXCEPT WED 4th 9:00-11:45 & 12:45-15:30

Teacher: Anna Kuparinen

Page 2: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

Course program

• Wed– Basics of R (AM & PM)

• Fri– Datasets in R (AM), graphics (PM)

• Mon– Statistical tests and linear models (AM), more graphics (PM)

• Tue– Basics of programming (AM), generalized linear models (PM)

• Wed– Mixed models (AM), working with own data (PM)

Teaching based on short lectures, demos, and exercises

Page 3: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

Course material

• Lectures, demo codes, exercises, and other material can be found by following the course link in

www.mv.helsinki.fi/home/akuparin/R_course.htm

• Other R material (e.g. downloading R)

http://www.r-project.org/

Page 4: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

How to get most out of the course?

• Do notes –also comment your own solution codes for the exercises

• Be active –ask questions, ask clarification, ask to slow down

• In the evenings, re-read the materials of the day• Start to use R immediately after the course

Page 5: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

What is ?

• R is a software that provides a very handy environment and a large collection of tools for– Exploring and editing data– Computations, calculations, numerical mathematics– Statistical analyses– Producing graphics– Programming

• R is a freeware program

• R is developed by its users, i.e. anyone can write their own R extensions and share them with other users through R web– New methods are quickly implemented to R– There is a large variety of tools available

Page 6: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

Getting started

R work space

Notepad

or R script

Write code first here!

Then copy-paste to R.

Page 7: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

How to use R?

• R is a text-based interface, i.e. commands are given by text lines– > one should remember at least most common text commands

• In practice, commands are typically written in an editor program and them copy-pasted to R– Notepad, R script, Tinn-R…– Commenting the code is highly recommended!

• R is based on objects and functions– Objects can be variables, lists, data frames…– Functions are made to carry out specific operations– Functions are stored in “library packages”

• R stores objects and commands on workspace– Workspace and command history can be saved and loaded later on– Large objects reserve a lot of memory in the workspace

Page 8: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

First commands

• Write your command on a command line

> 2+3

• Then press enter

[1] 5

• R returns the value

Page 9: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

Placing a value to a variable

• Write your command on a command line

> a=2+3

• Then press enter

>

• R does not return the value, but it has saved it to variable a

Page 10: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

Operators in R

= places a value (<- would also work)== checks identity of values on the left and on the right side

An command A=4/9 sets a value 4/9 to a variable called A.

An command A==4/9 checks if A equals to 4/9 or not. The command returns a logical value TRUE or FALSE

>= larger or of equal size<= smaller or of equal size

DEMO 1

Page 11: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

Vector

2.3

6.8

1.1

4.9

Length of this vector is 4.

1st element

4th element

Page 12: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

Vectors in R

• Generic way of creating a vector

> a=c(2.3,6.8,1.1,4.9)

• Pointing to a vector element

> a[1]

[1] 2.3

• Or several elements

> a[c(1,3)]

[1] 2.3 1.1

• Any calculations such as + or exp()can be applied to vectors.

Page 13: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

Matrix

2.3 8 9 7.66

5 3.3 14 2.2

7 0 5.4 7.2

1 1 3.8 8.2

A

1st column

1st row

A matrix element is pointed by its row and column number:

A[2,4]=-2.2

Size of this matrix is 4 X 4

Page 14: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

Matrix in R

• Generic way of creating a matrix

> a=matrix(c(2,5,6,1),nrow=2,ncol=2,byrow=T)

> a

[,1] [,2]

[1,] 2 5

[2,] 6 1

> a[1,]

[1] 2 5

> a[,2]

[1] 5 1

Page 15: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

Tools to create vectors and matrixes

• Tools to create vectors A = c(1.0,5.8,9.0)

A = 1:6

A = rep(1,9)

A = rep(c(1,2),4)

A = rep(c(1,2),each=4)

A = seq(1,10,by=0.2)

A = seq(1,10,length.out=57)

• Creating matrixes B = matrix(1,nrow=8,ncol=9)

B = matrix(0,nrow=9,ncol=4)

-> DEMO 2

Page 16: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

R as a calculator

• Basic math operators in R: +, -, /, *, ^

• Matrix calculations: – transpose matrixes by t()– Matrix calculations specified with %: for example * multiplies by

elements, and %*% is for multiplication of matrixes

• Basic mathematical functions:– log, exp, sqrt, cos, sin, tan, abs etc

• Rounding: round, floor, ceiling

Page 17: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

Anatomy of an R function

Object=function(parameters)

PARAMETER VALUES AND SETTINGS:

not all have to be defined

(see help files for the defaults)NAME OF THE

FUNCTION

OBJECT RETURNED BY THE FUNCTION

Page 18: Data, graphics, and programming in R 28.1, 30.1, 2-4.2 Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen

Help in R

• Many ways to find help:

– In R directly: ?”name of the function” or

help.search(“keyword”)

e.g. ?rep or help.search(“vector”)

– From R web pages

– For more advanced problems also from R discussion groups

– > DEMO 3