Learning Objectives

Names and Values

Lists and Data Frames

Character Vectors

  • A character vector is a vector of references to a global string pool.

    x <- c("a", "a", "abc", "d")

    character vector representation

  • But Hadley usually writes this as

    way we write character vectors

  • Use lobstr::ref() to show these references.

    lobstr::ref(x, character = TRUE)
    ## █ [1:0x555daaf628d8] <chr> 
    ## ├─[2:0x555da507a0e8] <string: "a"> 
    ## ├─[2:0x555da507a0e8] 
    ## ├─[3:0x555da952d810] <string: "abc"> 
    ## └─[4:0x555da522f6f0] <string: "d">
  • Exercise (from Advanced R): Why do you think x is copied here? (it is only copied twice if you use R studio). Modify the code so that x is not copied.

    x <- c(1L, 2L, 3L)
    tracemem(x)
    ## [1] "<0x555da888ed48>"
    x[[3]] <- 4
    ## tracemem[0x555da888ed48 -> 0x555da8a0a6b8]: eval eval withVisible withCallingHandlers handle timing_fn evaluate_call <Anonymous> evaluate in_dir eng_r block_exec call_block process_group.block process_group withCallingHandlers process_file <Anonymous> <Anonymous> withCallingHandlers suppressMessages render_one FUN lapply sapply <Anonymous> <Anonymous> 
    ## tracemem[0x555da8a0a6b8 -> 0x555da84ca6b8]: eval eval withVisible withCallingHandlers handle timing_fn evaluate_call <Anonymous> evaluate in_dir eng_r block_exec call_block process_group.block process_group withCallingHandlers process_file <Anonymous> <Anonymous> withCallingHandlers suppressMessages render_one FUN lapply sapply <Anonymous> <Anonymous>
    x <- c(1L, 2L, 3L)
    tracemem(x)
    ## [1] "<0x555da91516a8>"
    x[[3]] <- 4L
    ## tracemem[0x555da91516a8 -> 0x555da936e4a8]: eval eval withVisible withCallingHandlers handle timing_fn evaluate_call <Anonymous> evaluate in_dir eng_r block_exec call_block process_group.block process_group withCallingHandlers process_file <Anonymous> <Anonymous> withCallingHandlers suppressMessages render_one FUN lapply sapply <Anonymous> <Anonymous>
  • Exercise (From Advanced R): Sketch out the relationship between the following objects:

    a <- 1:10
    b <- list(a, a)
    c <- list(b, a, 1:10)

Object Size

Modify-in-place

Garbage Collection

New Functions


National Science Foundation Logo American University Logo Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.