Sie sind auf Seite 1von 21

Functions in R

Alena van Bmmel


Matthew Huska
Slides from Uwe Ziegenhagen

http://www.uweziegenhagen.de

Part 4

Agenda for Today

Programming R
Functions
Loops
Conditions
Packages

Functions in R

1-1

Part 4

Functions

1
2
3
4

1
2
3
4

myfun < - function ( x ) {


return ( x * x )
}
myfun (2)
myfun < - function ( x ) {
x*x
}
myfun (2)

If no return() is given, the object last created is returned.

Functions in R

1-2

Part 4

1-3

Functions

1
2
3
4
5

1
2

myfun < - function (x , a ) {


r < - a * sin ( x )
return ( r )
}
myfun ( pi /2 ,2)
myfun1 < - function (x , a ) { a * sin ( x ) } # short version
myfun1 ( pi /2 ,2)

Functions in R

Part 4

Functions

1
2
3
4
5
6

# optional parameter with default = 1


myfun2 < - function (x , a = 1) {
a * sin ( x )
}
myfun2 ( pi /2 ,2)
myfun2 ( pi /2)

Functions in R

1-4

Part 4

Functions

1
2
3
4
5
6
7
8
9
10

# optional parameter without default


myfun3 < - function (x , a = NULL ) {
if (! is . null ( a ) ) {
a * sin ( x )
} else {
cos ( x )
}
}
myfun3 ( pi /2 ,2)
myfun3 ( pi /2)

Functions in R

1-5

Part 4

Functions

Multiple return values have to be returned as a single list.


1
2
3
4
5
6

myfun4 < - function (x , a = 1) {


r1 < - a * sin ( x )
r2 < - a * cos ( x )
return ( list ( r1 , r2 ) ) # one list as result
}
myfun4 ( pi /2)

not possible: return(r1,r2)

Functions in R

1-6

Part 4

Loops and Conditions - IF


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# simple if
x <- 1
if ( x = = 2) { print ( " x = 2 " ) }
# if - else
x <- 1
if ( x = = 2) {
print ( " x = 2 " )
} else {
print ( " x ! = 2 " )
}
# ifelse function , takes a logical vector
> y < - sample . int (5)
[1] 5 3 1 4 2
ifelse ( y > 2 , " high " , " low " )
[1] " high " " high " " low " " high " " low "

Functions in R

1-7

Part 4

Loops and Conditions - FOR

1
2
3
4
5
6
7
8
9
10
11

for ( i in 1:4) { print ( i ) }


names < - c ( " Matt " , " Alena " , " Uwe " )
for ( name in names ) { print ( name ) }
for ( name _ idx in seq _ along ( names ) ) {
print ( paste ( name _ idx , name , sep = " : " ) )
}
data < - matrix ( sample . int (10) , ncol = 5)
# prefer seq _ len () rather than 1: ncol ( data )
for ( col _ idx in seq _ len ( ncol ( data ) ) ) {
data [ , col _ idx ] < - sort ( data [ , col _ idx ])
}

Functions in R

1-8

Part 4

1-9

Loops and Conditions - FOR


Loops in R code can be very slow and are often not necessary:
1
2

x < - data . frame ( matrix ( runif (100 * 1 e4 ) , ncol = 100) )


medians < - vapply (x , median , numeric (1) )

3
4
5
6
7
8

# subtract medians one element at a time in a loop


# takes 5.5 seconds
for ( i in seq _ along ( medians ) ) {
x [ , i ] < - x [ , i ] - medians [ i ]
}

9
10
11
12

# subtract a list of values


# takes 0.3 seconds
x < - x - as . list ( medians )

source: Hadley Wickham - Profiling


Functions in R

Part 4

Loops and Conditions - WHILE

1
2
3
4
5

i <- 0
while ( i < 4) {
i < - i +1
print ( i )
}

Do NOT forget the increase of the counter variable i!

Functions in R

1-10

Part 4

Loops and Conditions - REPEAT

1
2
3
4
5
6

i <- 0
repeat {
i < - i +1
print ( i )
if ( i = = 4) break
}

If no break is given, loop runs forever!

Functions in R

1-11

Part 4

The apply() commands

these commands allow functions to be run on matrices.


apply() Function used on matrix
tapply() table grouped by factors
lapply() on lists and vectors; returns a list
sapply() like lapply(), returns vector/matrix
mapply() multivariate sapply()

Functions in R

1-12

Part 4

1-13

apply()

apply(data, margin, function)


1
2
3
4
5
6
7

> a < - matrix (1:10 , nrow = 2)


> apply (a ,1 , mean ) # 1 = by rows
[1] 5 6
> apply (a ,2 , mean ) # 2 = by columns
[1] 1.5 3.5 5.5 7.5 9.5
# the function can also be anonymous
> apply (a ,2 , function ( x ) { x [[ sample . int ( length ( x ) ,1)
]] })

Functions in R

Part 4

lapply()

1
2
3
4
5
6

> a < - matrix (2:11 , nrow = 2)


> b < - matrix (1:10 , nrow = 2)
> c < - list (a , b )
> lapply (c , mean )
[[1]]
[1] 6.5

7
8
9
10
11

[[2]]
[1] 5.5
> sapply (c , mean )
[1] 6.5 5.5

Functions in R

1-14

Part 4

1-15

mapply()
Like sapply() but applies over the first elements of each argument
1
2
3
4

# mapply ( FUNCTION , list , list , list ...)


> mapply ( rep , pi , 3:1)
[[1]]
[1] 3.141593 3.141593 3.141593

5
6
7

[[2]]
[1] 3.141593 3.141593

8
9
10
11
12
13
14

[[3]]
[1] 3.141593
# equivalent to :
rep ( pi , 3)
rep ( pi , 2)
rep ( pi , 1)

Functions in R

Part 4

tapply()

Run a function on each group of values specified by a factor.


Requires a vector, factor and function.
1
2
3
4
5

> data ( iris )


> attach iris
> tapply ( Sepal . Width , Species , mean )
setosa versicolor virginica
3.428
2.770
2.974

Functions in R

1-16

Part 4

1-17

Errors and Debugging: debug()


1
2
3
4
5
6
7
8
9
10
11
12
13
14

> debug ( cat ) # disable debug flag : undebug ( cat )


> cat ( " hello " )
debugging in : cat ( " hello " )
debug : {
if ( is . character ( file ) )
if ( file = = " " )
file < - stdout ()
else if ( substring ( file , 1L , 1 L ) = = " | " ) {
file < - pipe ( substring ( file , 2 L ) , " w " )
on . exit ( close ( file ) )
}
...
\}
Browse [2] >

Functions in R

Part 4

1-18

Errors and Debugging: debug() 2

Debugger commands:
n advance to next step
c continue to the end of current loop or function
where print a stack trace
Q exit the browser
You can also use any R command! Check the value of variables,
length/dimensions of matrices, etc.

Functions in R

Part 4

1-19

Errors and Debugging: browser()

You can also start the debugger on a certain line of code, rather
than when a function is called:
1
2
3

mydata < - read . csv ( " testfile . csv " )


browser () # starts the debugger
means < - rowMeans ( mydata )

Functions in R

Part 4

Errors and Debugging

Start the debugger automatically when there is an error:


1
2

# put this at the top of your file


options ( error = recover )

Functions in R

1-20

Das könnte Ihnen auch gefallen