R Programming/Control Structures

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Contents

[edit] Conditional execution

  • Help for programming :
> ?Control


if (condition){
statement  
}
else {
alternative
}
  • The ifelse() command takes as first argument the condition, as second argument the treatment if the condition is true and as third argument the treatment if the condition is false.
    • For instance we generate a sequence from 1 to 10 and we want to display values which are lower than 5 and greater than 8.
> x <- 1:10 
> ifelse(x<5 | x>8, x, 0)
 [1]  1  2  3  4  0  0  0  0  9 10

[edit] Loops

R provides three ways to write loops: for, repeat and while.

  • The for statement is excessively simple. You simply have to define index (here k) and a vector (in the example below the vector is 1:5) and you specify the action you want between braces.
> for (k in 1:5){
+ print(k)
+ }
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5


  • When it is not possible to use the for statement, you can also use break or while by specifying a breaking rules. One should be careful with this kind of loops since if the breaking rules is misspecified the loop will never end.

In the two examples below the standard normal distribution is drawn in as long as the value is lower than 1. The cat() function is used to display the present value on screen.

> repeat { 
+ 	g <- rnorm(1) 
+ 	if (g > 1.0) break 
+ 	cat(g,"\n")
+ 	} 
-1.214395 
0.6393124 
0.05505484 
-1.217408 
> g <- 0
> while (g < 1){
+ 	g <- rnorm(1) 
+ 	cat(g,"\n")
+ 	}
-0.08111594 
0.1732847 
-0.2428368 
0.3359238 
-0.2080000 
0.05458533 
0.2627001 
1.009195


[edit] Iterators

[edit] Implicit loops

  • 'apply'

[edit] Functions

> fn <- function(){
+ print("hello")
+ }
> fn()
[1] "hello"

[edit] Computing time

If you perform computer intensive task you may want to optimize the computing time. Two functions are available system.time() and proc.time(). Both returns a vector of values. The first is the standard CPU time

> system.time(x<-rnorm(10^6))
[1] 1.14 0.07 1.83 0.00 0.00
> debut <- proc.time()
> x <- rnorm(10^6)
> proc.time()-debut
[1]  1.66  0.10 10.32  0.00  0.00

[edit] Linking C with R

[edit] Unix-like functions

  • system() gives access to the system (DOS or unix). For instance, you can convert an image from to PS to PNG using the unix convert function of your computer. If you want to know more about this function, open a Terminal application and type man convert (This should work on Mac OS and Linux).
system("convert W:/toto.ps W:/toto.png")
  • file.info() gives information about a file.
> file.info("taille.txt")
           size isdir mode               mtime               ctime               atime exe
taille.txt  444 FALSE  666 2009-06-26 12:25:44 2009-06-26 12:25:43 2009-06-26 12:25:43  no
  • getZip() in the Hmisc package.


Removing files with a specific pattern :

file.remove(dir(path="directoryname", pattern="*.log")) 
  • See the R.utils package[1]

[edit] References

  1. Henrik Bengtsson (2009). R.utils: Various programming utilities. R package version 1.1.7. http://CRAN.R-project.org/package=R.utils
Previous: Random Number Generation Index Next: Data