---------------------------------------------------------------- HELP: Function documentation, of fct 'c()', e.g.: help("c") or help(c) Related functions: "See Also:" in help(...) Functions whose name contains "char", e.g.: apropos("char") Args and body of a fct, 'paste', e.g.: paste; print(paste) Syntax: help("Syntax") ---------------------------------------------------------------- ASSIGNMENT: x <- c(3,5); x = c(3,5) ---------------------------------------------------------------- MANAGEMENT OF DATA STRUCTURES AND FUNCTIONS: listing data & fcts: ls(); objects() # identical ls("package:base") # contents of base package ls(pattern="last") # names containing "last" installed packages: search() # package = namespace removing data strutures: a <- 1:10; b <- 2*a; rm(a,b); ls() shadowing, of 'ls', e.g.: ls <- 10; ls; rm(ls); ls ---------------------------------------------------------------- BASE DATA TYPES: Numeric: 1; 1E10; 3.2; -5.67; pi String/character: "a"; "ab#ba10"; letters; LETTERS Logical: T; TRUE; F; FALSE Missing: NA; NaN; Inf; -Inf ---------------------------------------------------------------- VECTORS: a high-level data structure others: matrices, arrays, lists, dataframes Concatenations: c(1,2,3); c(NA,2,Inf) c("a","ab","A") c(T,F,T,F) a <- c(1,10,100); c(a,200,300); c(a,200,a) Ladders: 1:3; -2:2; -2:-5 seq(1,3); seq(1,3,by=1); seq(1,3,length=3) seq(0,1,by=0.1); seq(-1,1,length=101) Repetitions: - simple rep: rep(pi,3); rep("a",3); rep(T,3); rep(1,1000) - vector rep: rep(1:3,3); rep(letters,2); rep(c(T,F,F),3) - element rep: rep(1:3,rep(3,3)); rep(c(1,10,100),1:3) Coercion: T/F ==> Numeric ==> Character c(F,1); c(1,"a"); c(F,"a"); c(F,1,"a") Cyclic extension: rep(1,5)+0:1; paste(letters,c("-","+")) ---------------------------------------------------------------- VECTORIZATION: Most operations and functions apply to vectors element by element!!!!! Examples: c(1,10,100)+1:3; 2^(0:10) round(rnorm(20)); paste(c("a","z"),"-") ---------------------------------------------------------------- NUMERICS: a <- c(2,4); b <- c(-2.5,1.5) # play vectors Arithmetic ops: -a; 1+b; a+b; a-1; 2*3; 6/2; 10%/%3; 10%%3; 2^a (a+1)^(-2) Arithmetic fcts: a <- c(1.5,-1.25); round(a); round(a,1); signif(a,2) ceiling(a); trunc(a) Analytic fcts: sqrt(9); exp(pi); log(a); log10(10); log2(8); Trigonometrics: sin(pi/2); cos(pi/2); tan(pi/4) asin(1); acos(1); atan(1) ---------------------------------------------------------------- CHARACTERS/STRINGS: paste(c("a","b"),c("x","y")); paste(letters,LETTERS) separator: paste(c("a","b"),c("x","y"), sep="") paste(c("a","b"),c("x","y"), sep="->") multiple args: paste(letters,LETTERS,1:length(letters)) cyclic rep: paste(c("a","b"),"...") paste(letters,c("-> odd","-> even")) collapse: paste(letters, collapse="") paste(LETTERS, collapse="-->") coercion: paste(1:9, collapse="--") substrings: substring("aabbccdd",3,6) substring(c("abc","uvwxyz"),2,3) string length: nchar(c("a","xxxxxxxxxx")) splitting: strsplt() #x pattern matching: grep() #x ---------------------------------------------------------------- LOGIC: - equality: 10==pi; 10!=pi; (1:10)==5; "a"=="b"; letters=="w" - strict ineq: -2>3.2; -2< -1 (space needed! '<-'); "ab"<"bbb" - nonstrict ineq: -2>=3.5; -2<=3.5; "zzz"<="zz"; letters<="h" - logic ops: x <- c(F,T); !x; !T; x&T; x|T; y <- T; (x|F)&F - lazy logic: ||; && #x ---------------------------------------------------------------- LOOPS: for(i in (1:5)*2) { print(2^i); print("great!") } for(i in 1:3) for(j in 1:3) print(c(i,j)) x <- c(1,2,3,4,3,2,1); for(xx in x) print(rep("*",xx)) while repeat apply() lapply() tapply() ---------------------------------------------------------------- CONDITIONALS: if() {} #x if() {} else {} x <- ifelse(,,) ---------------------------------------------------------------- INDEXING VECTORS: a <- 1:10+100 # vector to play with - single elements: a[2]; letters[10] - subvectors: a[c(8,1)]; LETTERS[11:15] - rearrangements: a[c(1,2,1,2,1,2,3,3,3)]; letters[c(1,26,1,26,13,13)] - exclusion: a[-3]; a[-c(1,10)]; letters[-(1:13)] - extension: a[20:25] <- 0:5 - logical: (1:5)[c(F,T,F,F,T)]; letters[c(F,T,F)] # cyclic rep. a[a>105 & a%%2==0] ---------------------------------------------------------------- PARTIAL ASSIGNMENT: - vectors: a[4:6] <- -a[4:6]; a[a%%3==1] <- 0 - characters: b <- "abcdefghijk"; substring(b,4,6) <- "uvwxyz" ---------------------------------------------------------------- ASSOCIATIVE ARRAYS: - creation: a <- c(first=10, second=100, third=-1000) - names attrib: names(a) - assigning names: a <- c(10,100,-1000) names(a) <- c("first","second","third"); a - single: a["third"] - multiple: a[c("first","second")] - rearrangement: a[c("first","second","second","first")] ---------------------------------------------------------------- ORDER: a <- c(1,8,2,7,6,3,5,4); b <- 8:1 - simple sort: sort(a); sort(c("z","za","az")) - parallel sort: ord <- order(a); a[ord]; b[ord] - reverse: rev(1:8); rev(c(T,F,T,F)) - permute randomly: sample(a); sample(LETTERS) - permute randomly & parallel: ord <- sample(1:length(a)); a[ord]; b[ord] ---------------------------------------------------------------- RANDOM NUMBERS: - uniform: runif(10); runif(n=5,min=-1,max=1) runif(10, 0:9, 1:10) - normal: rnorm(10); rnorm(n=10,m=100,s=10) rnorm(10,1:10*100) - Bernoulli(0.5): runif(10)>.5 ---------------------------------------------------------------- SAMPLING: a <- (1:10)*2 - without replacement: sample(a, 8) - with replacement: sample(a, 8, replace=T) - random permutations: sample(a) # special case of 'without replacement' ---------------------------------------------------------------- PRINTING: unformatted printing: print(pi); print(1:10); print(letters) formatted printing: cat("abc\n") cat(signif(pi,3),"\n") cat(letters,"\n") cat("\n","a","\n","aa","\n","aaa","\n") ---------------------------------------------------------------- INPUT: reading as a numeric vector: scan("sp.dat",n=10) # expects numeric a <- scan("sp.dat"); length(a); rm(a) reading as a character vector: scan("sp.dat",n=10,what="") change separator to newline: scan("laser.dat",w="",sep="\n") reading a data frame: read.table() #x reading a csv table: read.csv() #x ---------------------------------------------------------------- PERSISTENCE OF DATA AND DEFAULTS: saving data for subsequent sessions: q() # answer "y" setting defaults: .First <- function() { options(width=100) } # executed at the start of every session ---------------------------------------------------------------- FUNCTIONS: ----------------------------------------------------------------