Rcode

## Remember! ###
> 5:1
[1] 5 4 3 2 1
The any() and all() functions are handy:
x 8)) print("yes")
[1] "yes"
if (any(x > 88)) print("yes")
if (all(x > 88)) print("yes")
if (all(x > 0)) print("yes")
[1] "yes"
# vectorization
u <- br="br" c="c"> v v
[1] TRUE FALSE FALSE</->
# Don't use loops!
lv <- br="br" length="length" v="v"> outvec <- length="lv)<br" vector="vector"> for (i in 1:lv) {
outvec[i] <- br="br" f="f" i="i" v="v"> }</-></-></->
Instead treat the vector as a matrix and using the apply() function:
outvec <- apply="apply" as.matrix="as.matrix" f="f" p="p" v="v"># Filtering
> z <- br="br" c="c"> > w <- z="z"> 8]
> w
[1] 5 -3 8
# finding the positions:</-></->
> which(z*z > 8)
[1] 1 3 4
# changing elements of a vector
> x <- br="br" c="c"> > x[x > 3] <- 0="0" br="br"> > x
[1] 1 3 0 2</-></->
foobie <- readClipboard() #####################################################################################
# Nice example of a compound command
nrow(subset(x03,z==1))
# sourcing a final script from the command line
R CMD BATCH --vanilla < z.r
# predeclaring, might be useful
y <- length="2)<br" vector="vector"> y[1] <- 5="5" br="br"> y[2] <- 12="12" p="p"># !!! Suppose we have a sequence of numbers for which we want to find successive differences, i.e. the difference between each number and its predecessor. Here’s how we could do it:
x <- br="br" c="c"> y <- -="-" br="br" length="length" x="x"> y
[1] 3 -7 3 13</-></->
#We can reference elements of the vector by name, e.g.
x <- br="br" c="c"> names(x) <- a="a" ab="ab" b="b" br="br" c="c"> x["b"]
b
2</-></-></-></-></-></->