commit ec7c26d7f08256bcb7006b6189bd11cba6f5b5fc
parent ee09dc2380e8655113fa654f2330c3d6dc2fc910
Author: Federica Gazzelloni (she/her) <61802414+Fgazzelloni@users.noreply.github.com>
Date:   Fri,  9 Feb 2024 13:04:18 +0100
Ch2 (#56)
* chapter2
* updating chapter2
* ### to ##
* ### to ##
* ### to ##
* ### to ##
* ### to ##
* ### to ##
* ### to ##
* ### to ##
* ### to ##
---------
Co-authored-by: Jon Harmon <jonthegeek@gmail.com>
Diffstat:
2 files changed, 144 insertions(+), 4 deletions(-)
diff --git a/02_Names_and_values.Rmd b/02_Names_and_values.Rmd
@@ -2,12 +2,152 @@
 
 **Learning objectives:**
 
-- THESE ARE NICE TO HAVE BUT NOT ABSOLUTELY NECESSARY
+- test your knowledge
+- identify objects in R (size, id, ...)
+
+
+## Quiz {-}
+
+1. How do I create a new column called "3" that contains the sum of 1 and 2?
+```{r}
+df <- data.frame(runif(3), runif(3))
+df
+```
+
+
+```{r}
+names(df) <- c(1, 2)
+```
+```{r}
+df$`3`
+```
+
+
+```{r}
+df$`3` <- df$`1` + df$`2`
+
+df
+```
+
+
+2. How much memory does y occupy?
+hint: use `library(lobstr)` 
+```{r}
+x <- runif(1e6)
+y <- list(x, x, x)
+
+length(y)
+
+```
+
+
+```{r}
+library(lobstr)
+lobstr::obj_size(y)
+```
+
+
+3. On which line does a get copied in the following example?
+```{r}
+a <- c(1, 5, 3, 2)
+a
+```
+
+
+```{r}
+b <- a
+b
+```
+
+
+```{r}
+b[[1]] <- 10
+```
+
+```{r}
+b
+```
+
+
+## Object’s identifier {-}
+
+```{r}
+x <- c(1, 2, 3)
+obj_addr(x)
+```
+
+
+## Exercises {-}
+
+Do they all point to the same underlying function object? hint: `lobstr::obj_addr()`
+```{r}
+mean
+base::mean
+get("mean")
+evalq(mean)
+match.fun("mean")
+```
+
+
+## Copy-on-modify {-}
+```{r}
+x <- c(1, 2, 3)
+cat(tracemem(x), "\n")
+#> <0x7f80c0e0ffc8> 
+```
+
+> untracemem() is the opposite of tracemem(); it turns tracing off.
+
+```{r}
+y <- x
+
+y[[3]] <- 5L
+
+untracemem(x)
+```
+
+## Introduction to functions {-}
+
+How to make a function in r:
+```{r eval=FALSE}
+name <- function(variables) {
+  
+}
+```
+
+```{r}
+f <- function(a) {
+  a
+}
+
+x <- c(1, 2, 3)
+cat(tracemem(x), "\n")
+#> <0x7fe1121693a8>
+
+z <- f(x)
+# there's no copy here!
+
+untracemem(x)
+```
+
+
+
+## Lists {-}
+```{r}
+l1 <- list(1, 2, 3)
+```
+
+## Data Frames {-}
+```{r}
+d1 <- data.frame(x = c(1, 5, 6), y = c(2, 4, 3))
+```
+
+## Character vectors {-}
+```{r}
+x <- c("a", "a", "abc", "d")
+```
 
-## SLIDE 1
 
-- ADD SLIDES AS SECTIONS (`##`).
-- TRY TO KEEP THEM RELATIVELY SLIDE-LIKE; THESE ARE NOTES, NOT THE BOOK ITSELF.
 
 ## Meeting Videos
 
diff --git a/images/02-trace.png b/images/02-trace.png
Binary files differ.