R programming language/Scripts

From Christoph's Personal Wiki
Revision as of 23:06, 3 August 2007 by Christoph (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
see: main R programming language article

Creating shaded areas in R

cord.x<-c(-3,seq(-3,-2,0.01),-2)
cord.y<-c(0,dnorm(seq(-3,-2,0.01)),0)
curve(dnorm(x,0,1),xlim=c(-3,3),main='Example Normal Curve')
polygon(cord.x,cord.y,col='grey')

DNA Microarray Analysis - Example

## Objects
x <- rnorm(30)
y <- x[x>0]
z <- x
z[z<0] <- 0

m <- matrix(x, nrow = 5)
str(m)

d.f <- as.data.frame(m)
str(d.f)

m[2,2] = "a"
d.f[2,2] = "a"
str(m)
str(d.f)

## Functions

cube <- function(x) {
  z <- x*x*x
  return(z)
}

fact <- function(x) {
z <- 1
  for (i in 2:x) {
    z <- z * i
  }
  return(z)
}

func <- function(x, y) {
  z <- cube(x) - fact(y)
  return(z)
}

## Graphics

hist(a <- rnorm(100))

X11()
plot(a <- rnorm(100), b <- rnorm(100))
points(a[a<0 & b>0], b[a<0 & b>0],col="green")
points(a[a>0 & b>0], b[a>0 & b>0],col="red")
points(a[a>0 & b<0], b[a>0 & b<0],col="blue")
points(a[a<0 & b<0], b[a<0 & b<0],col="yellow")
lines(c(-10,10),c(0,0))
lines(c(0,0),c(-10,10))