Skip to content

Commit

Permalink
Ch2 (#56)
Browse files Browse the repository at this point in the history
* chapter2

* updating chapter2

* ### to ##

* ### to ##

* ### to ##

* ### to ##

* ### to ##

* ### to ##

* ### to ##

* ### to ##

* ### to ##

---------

Co-authored-by: Jon Harmon <[email protected]>
  • Loading branch information
Fgazzelloni and jonthegeek authored Feb 9, 2024
1 parent ee09dc2 commit ec7c26d
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 4 deletions.
148 changes: 144 additions & 4 deletions 02_Names_and_values.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

![](images/02-trace.png)

## 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

Expand Down
Binary file added images/02-trace.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ec7c26d

Please sign in to comment.