# Today I Learned: ____

#### derksheng

##### New Member
The little things that really revolutionised my R:

which()
grep()
strsplit()

and some other stuff

#### Dason

TIL: You an recursively access lists with "[[" indexing. I thought you could only access a single element of a list using "[[" but I didn't realize that if you pass in a vector it will recursively work it's way down the list...

For example
Code:
o[[c(1,2,3)]]
# is the same as
o[[1]][[2]][[3]]
Some concrete examples...
Code:
> x <- 1:10
> y <- rnorm(10)
> o <- lm(y ~ x)
> str(o)
List of 12
$coefficients : Named num [1:2] -0.0931 -0.013 ..- attr(*, "names")= chr [1:2] "(Intercept)" "x"$ residuals    : Named num [1:10] -0.328 0.236 0.574 -1.261 0.299 ...
..- attr(*, "names")= chr [1:10] "1" "2" "3" "4" ...
$effects : Named num [1:10] 0.52 -0.118 0.614 -1.194 0.393 ... ..- attr(*, "names")= chr [1:10] "(Intercept)" "x" "" "" ...$ rank         : int 2
$fitted.values: Named num [1:10] -0.106 -0.119 -0.132 -0.145 -0.158 ... ..- attr(*, "names")= chr [1:10] "1" "2" "3" "4" ...$ assign       : int [1:2] 0 1
$qr :List of 5 ..$ qr   : num [1:10, 1:2] -3.162 0.316 0.316 0.316 0.316 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$: chr [1:10] "1" "2" "3" "4" ... .. .. ..$ : chr [1:2] "(Intercept)" "x"
.. ..- attr(*, "assign")= int [1:2] 0 1
..$qraux: num [1:2] 1.32 1.27 ..$ pivot: int [1:2] 1 2
..$tol : num 1e-07 ..$ rank : int 2
..- attr(*, "class")= chr "qr"
$df.residual : int 8$ xlevels      : Named list()
$call : language lm(formula = y ~ x)$ terms        :Classes 'terms', 'formula' length 3 y ~ x
.. ..- attr(*, "variables")= language list(y, x)
.. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$: chr [1:2] "y" "x" .. .. .. ..$ : chr "x"
.. ..- attr(*, "term.labels")= chr "x"
.. ..- attr(*, "order")= int 1
.. ..- attr(*, "intercept")= int 1
.. ..- attr(*, "response")= int 1
.. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. ..- attr(*, "predvars")= language list(y, x)
.. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. ..- attr(*, "names")= chr [1:2] "y" "x"
$model :'data.frame': 10 obs. of 2 variables: ..$ y: num [1:10] -0.434 0.117 0.442 -1.406 0.141 ...
..$x: int [1:10] 1 2 3 4 5 6 7 8 9 10 ..- attr(*, "terms")=Classes 'terms', 'formula' length 3 y ~ x .. .. ..- attr(*, "variables")= language list(y, x) .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1 .. .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. .. ..$ : chr [1:2] "y" "x"
.. .. .. .. ..$: chr "x" .. .. ..- attr(*, "term.labels")= chr "x" .. .. ..- attr(*, "order")= int 1 .. .. ..- attr(*, "intercept")= int 1 .. .. ..- attr(*, "response")= int 1 .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> .. .. ..- attr(*, "predvars")= language list(y, x) .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric" .. .. .. ..- attr(*, "names")= chr [1:2] "y" "x" - attr(*, "class")= chr "lm" > o[["qr"]]$rank
[1] 2
> # same as
> o[[c("qr", "rank")]]
[1] 2
>
> coef(o)[["x"]]
[1] -0.01298689
> o[[c("coefficients", "x")]]
[1] -0.01298689

#### trinker

##### ggplot2orBust
@Dason That's really handy. Nice share!

#### Dason

TIL: Two functions

Code:
> timestamp()
##------ Tue Jul 17 20:23:12 2012 ------##
timestamp is useful inside simulations. I usually do something like print(date()) or print(paste(i, format(date()))) but I think timestamp() is a little nicer.

And the other thing I learned which I don't see myself using anytime soon since I can't figure out a good way to catch the error it throws without wrapping everything in a big block... is the function setTimeLimit.

Code:
f <- function(){
setTimeLimit(elapsed = 5)
for(i in 1:7){
timestamp()
Sys.sleep(1)
}
return("Function exited")
}
Code:
> f()
##------ Tue Jul 17 20:25:59 2012 ------##
##------ Tue Jul 17 20:26:00 2012 ------##
##------ Tue Jul 17 20:26:01 2012 ------##
##------ Tue Jul 17 20:26:02 2012 ------##
##------ Tue Jul 17 20:26:03 2012 ------##
Error in Sys.sleep(1) : reached elapsed time limit

#### trinker

##### ggplot2orBust
Good ol' Bill Dunlap put some code up for accessing elements from dots (...) that traditionally used match.call. His method requires less processing:
Code:
f1 <- function(x, ...) substitute(...())                   #Dunlap's method