Skip to content

Commit b9a6c19

Browse files
committed
Updated lut example to be more readable.
1 parent 21fa19f commit b9a6c19

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

CodeTricks.md

+7-13
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,17 @@ Accesssing arrays via index is faster than by name, but using names can make for
4646
To achieve fast access and still use names, one can construct an environment that uses a hash table for lookup
4747

4848
```R
49-
labeled.environment <- function(n) {
50-
e <- new.env(hash=TRUE,size=n)
51-
from <- "0123456789"
52-
to <- "ABCDEFGHIJ"
53-
for (i in 1:n) {
54-
assign(x=chartr(from,to,i), value=i, envir=e)
55-
}
56-
e
57-
}
58-
e.20 <- labeled.environment(20)
49+
e <- new.env(hash=TRUE,size=2)
50+
assign(x="first", value=1, envir=e)
51+
assign(x="second", value=2, envir=e)
52+
e
5953

6054
## Access via
61-
e.20[["B"]]
62-
get("BH", env=e.20)
55+
e[["first"]]
56+
get("second", env=e)
6357
```
6458

65-
This tip is from [Joseph Adler](http://broadcast.oreilly.com/2010/03/lookup-performance-in-r.html)
59+
This tip is from [Joseph Adler](http://broadcast.oreilly.com/2010/03/lookup-performance-in-r.html), who goes throught the speed improvements of this method verus named arrays.
6660

6761

6862
## Compiling code

0 commit comments

Comments
 (0)