|
|
@@ -28,3 +28,55 @@ plot(pressure) |
|
|
|
``` |
|
|
|
|
|
|
|
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. |
|
|
|
|
|
|
|
## Environments |
|
|
|
|
|
|
|
|
|
|
|
R environments are used to bind names against values. In |
|
|
|
this case the subjectid is the name. |
|
|
|
|
|
|
|
```{r} |
|
|
|
id <- new.env() |
|
|
|
id$16K0021 = 'first' |
|
|
|
``` |
|
|
|
|
|
|
|
oops, can't start with a number |
|
|
|
|
|
|
|
```{r} |
|
|
|
id$m16K0021 = 'first' |
|
|
|
ls(id, all.names = TRUE) |
|
|
|
``` |
|
|
|
|
|
|
|
Note that data frames use environment too with the $ notation. |
|
|
|
|
|
|
|
To add material to an environment we create a new environment (a hash of hash). So |
|
|
|
|
|
|
|
```{r} |
|
|
|
mydata <- new.env() |
|
|
|
mydata$days = 21 |
|
|
|
mydata$name = "Pjotr Prins" |
|
|
|
id$m16K0021 = mydata |
|
|
|
ls(id$m16K0021, all.names = TRUE) |
|
|
|
id$m16K0021$name |
|
|
|
``` |
|
|
|
|
|
|
|
That works well. Step is to be able to parametrize |
|
|
|
|
|
|
|
```{r} |
|
|
|
id[["m16K0021"]]$days |
|
|
|
``` |
|
|
|
|
|
|
|
So you can see the dollar notation is just syntactic sugar. |
|
|
|
|
|
|
|
```{r} |
|
|
|
id[["m16Ktest"]] = new.env() |
|
|
|
test = id[["m16Ktest"]] |
|
|
|
test$name = "Jan Wolkers" |
|
|
|
test$days = 22 |
|
|
|
ls(id, all.names = TRUE) |
|
|
|
ls(test, all.names=TRUE) |
|
|
|
test$name |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
See also http://adv-r.had.co.nz/Environments.html |