bookclub-advr

DSLC Advanced R Book Club
git clone https://git.eamoncaddigan.net/bookclub-advr.git
Log | Files | Refs | README | LICENSE

09.qmd (9711B)


      1 ---
      2 title: Cohort 9
      3 ---
      4 
      5 {{< video https://www.youtube.com/embed/gwsc8fjMqXQ >}}
      6 
      7 <details>
      8 
      9 <summary>Meeting chat log</summary>
     10 ```
     11 00:10:23	Steffi LaZerte (she/her):	start
     12 00:16:13	Steffi LaZerte (she/her):	isTRUE(x) is a really specific test of whether something is TRUE
     13 00:17:49	Joel Liebert:	Thanks Steffi, I wasn't familiar with that function. From the docs: "isTRUE(x) is the same as { is.logical(x) && length(x) == 1 && !is.na(x) && x }"
     14 00:17:58	Steffi LaZerte (she/her):	Reacted to "Thanks Steffi, I was..." with 👍🏻
     15 00:19:11	Steffi LaZerte (she/her):	This was brand new to me! I had no idea that the implied FALSE was NULL.
     16 00:19:12	Derek Sollberger (he/him):	neat! … I hadn't heard that function called "paste oh" before
     17 00:19:43	Steffi LaZerte (she/her):	paste0() changed my life :🤣
     18 00:20:00	Gabby Palomo:	I have always had problems coming out in ways to use if else etc in my functions. I guess the best example is the function if the package is not installed then install it. But I was wondering if anyone else had examples on your blog or GitHub please share them with me here or on Slack. I feel like if I see examples then I will start to see how to use them in my own functions.
     19 00:20:25	Gabby Palomo:	paste0 for the win. But I want to start using glue too.
     20 00:20:34	Steffi LaZerte (she/her):	Reacted to "paste0 for the win. ..." with 👍🏻
     21 00:21:51	Derek Sollberger (he/him):	Replying to "I have always had pr..."
     22 
     23 as a teacher, I tend to use if_else() en route to teaching about dummy variables (one-hot encoding)
     24 
     25 new_col = if_else(chr_vec == "this label", 1, 0)
     26 00:22:18	tataphani:	Are there any difference between paste and glue we can discuss later
     27 00:22:19	Gabby Palomo:	Reacted to "as a teacher, I tend..." with 💯
     28 00:22:40	tataphani:	sure
     29 00:23:33	Steffi LaZerte (she/her):	Replying to "I have always had pr..."
     30 
     31 I use it a lot when evaluating different arguments in a function. So if the function could have an argument of blue or green, I'll use an ifelse() to deal with the switch. Or if the argument is something like "total" vs. "summary", I'll use if() {} else {} to do different calculations based on that argument. Is this the kind of example you were thinking about?
     32 00:24:29	Steffi LaZerte (she/her):	If anyone uses chunk options in Quarto/Rmarkdown, I always make the mistake of forgetting that in THOSE cases it really is 'true' and not TRUE :)
     33 00:24:36	Gabby Palomo:	Replying to "I have always had pr..."
     34 
     35 I totally see the total and summary!! You are right, perfect example to use if else etc. I see this. YEs this is what I'm looking for.
     36 00:24:59	Gabby Palomo:	Replying to "If anyone uses chunk..."
     37 
     38 yeah in the yaml too
     39 00:25:00	Derek Sollberger (he/him):	Reacted to "I use it a lot when ..." with 😻
     40 00:25:08	Steffi LaZerte (she/her):	Reacted to "I totally see the to..." with 👍🏻
     41 00:25:25	Steffi LaZerte (she/her):	Reacted to "yeah in the yaml too" with 💯
     42 00:27:14	Leo:	our conclusion from the first 2 sections is that "if (anytext)" will always return FALSE?
     43 00:30:03	Steffi LaZerte (she/her):	I'm glad you brought this up! I totally missed the idea of cascading 'true' to TRUE, yikes!
     44 00:30:29	tataphani:	Replying to "our conclusion from ..."
     45 
     46 is it true for other
     47 00:30:37	tataphani:	Replying to "our conclusion from ..."
     48 
     49 like FALSE
     50 00:30:42	tataphani:	Replying to "our conclusion from ..."
     51 
     52 or NA
     53 00:31:03	tataphani:	Replying to "our conclusion from ..."
     54 
     55 or something
     56 00:35:14	tataphani:	Replying to "our conclusion from ..."
     57 
     58 it is like CASE and WHEN in SAS
     59 00:35:36	tataphani:	Replying to "our conclusion from ..."
     60 
     61 ?
     62 00:38:00	Steffi LaZerte (she/her):	I think you can quote or not the switch labels (like a named vector or list)
     63 00:39:05	tataphani:	Replying to "our conclusion from ..."
     64 
     65 never mind
     66 00:41:15	Steffi LaZerte (she/her):	dplyr::case_match() is another option, more similar to base switch() (but I can't say I'm very comfortable using it!)
     67 00:43:47	Gabby Palomo:	Reacted to "dplyr::case_match() ..." with 🤯
     68 00:46:44	Gabby Palomo:	That explained it perfectly Steffi!!
     69 00:47:26	Gabby Palomo:	I don't want to rush things because I love these discussions but it's 40 min after the hour.
     70 00:48:30	Leo:	the .default argument is used to set what NA would return?
     71 00:49:13	Steffi LaZerte (she/her):	It would return the value if none of the other options matched
     72 00:49:33	tataphani:	soory
     73 00:49:54	Steffi LaZerte (she/her):	I'm just as bad 😁  Just means it's a good discussion!
     74 00:50:37	Diana Garcia Cortes:	Small example in the iris dataset: 
     75 as_tibble(iris) %>% 
     76   janitor::clean_names() %>%
     77   mutate(
     78     is_setosa = case_match(species, "setosa" ~ "yes", .default = "no"),
     79     big_sepal = case_when(sepal_length > 5  & sepal_width > 3 ~ "big", .default = "small")
     80   )
     81 00:50:55	Steffi LaZerte (she/her):	Reacted to "Small example in the..." with 💯
     82 00:50:57	Gabby Palomo:	Oh I love them too!! Don't get me wrong, but maybe we can save some of them and review them all at the end once we've gone over the chapter.
     83 00:51:01	Derek Sollberger (he/him):	Reacted to "Small example in the..." with 👍
     84 00:51:09	Steffi LaZerte (she/her):	Reacted to "Oh I love them too!!..." with 👍🏻
     85 00:51:23	Gabby Palomo:	Reacted to "Small example in the..." with 💯
     86 00:52:10	tataphani:	Reacted to "Small example in the..." with 👍
     87 00:52:17	Gabby Palomo:	I love using i j k it makes me feel like a mathematician LOL
     88 00:52:24	Gabby Palomo:	Don't take them from me!!!
     89 00:52:36	Diana Garcia Cortes:	Reacted to "I love using i j k i..." with ❤️
     90 00:52:41	Steffi LaZerte (she/her):	Reacted to "Don't take them from..." with 🤣
     91 00:56:18	Gabby Palomo:	seq_along is similar to using length(vector) ?
     92 00:56:40	Steffi LaZerte (she/her):	And safer
     93 00:56:57	Gabby Palomo:	switching to seq_along() from now on
     94 00:57:00	Leo:	is it correct that for loop can pretty much be replaced by ifelse statement? and usually ifelse would be faster?
     95 00:57:07	Steffi LaZerte (she/her):	I always get it mixed up with seq_len() though
     96 00:57:39	Steffi LaZerte (she/her):	Replying to "is it correct that f..."
     97 
     98 Not quite. A For loop is more about repitition rather than choices
     99 00:57:41	Gabby Palomo:	Oh no... what? So what's the difference?
    100 01:00:22	Steffi LaZerte (she/her):	Replying to "is it correct that f..."
    101 
    102 So a for loop repeats a set of code over and over, but a if/else does code 1 if true and code 2 if false, but only once
    103 01:00:26	Diana Garcia Cortes:	seq_len(iris) -> error
    104 01:00:35	Diana Garcia Cortes:	seq_along(iris)
    105 [1] 1 2 3 4 5 6 7
    106 01:00:36	Gabby Palomo:	YES MAP AND APPLY!!
    107 01:00:37	Steffi LaZerte (she/her):	Reacted to "seq_len(iris) -> err..." with 😞
    108 01:00:40	Gabby Palomo:	I was gonna ask that
    109 01:00:57	Diana Garcia Cortes:	Reacted to "          YES MAP AN..." with 👍
    110 01:00:58	Steffi LaZerte (she/her):	Replying to "seq_along(iris)
    111 [1] ..."
    112 
    113 seq_len(nrow(iris))
    114 01:01:10	Steffi LaZerte (she/her):	Replying to "seq_along(iris)
    115 [1] ..."
    116 
    117 > seq_len(nrow(iris))
    118 [1] 1 2 3 4 5 6 7 8 ....
    119 01:01:24	Joel Liebert:	repeat() seems similar to  `while(true)` 
    120 
    121 x <- 1
    122 repeat {
    123   print(x)
    124   x = x+1
    125   if (x == 6){
    126     break
    127   }
    128 }
    129 01:02:19	Leo:	Replying to "is it correct that f..."
    130 
    131 thank you
    132 01:02:54	Derek Sollberger (he/him):	When _apply was faster than loops, it was maybe back when loops created new value spaces at each iteration? (referring back to chapter 2)
    133 01:04:10	Steffi LaZerte (she/her):	Replying to "When _apply was fast..."
    134 
    135 Yes! I was wondering about that too! I had also heard that for loops used to be less efficient but that had changed.
    136 01:04:21	Gabby Palomo:	Reacted to "When _apply was fast..." with 💯
    137 01:05:41	Gabby Palomo:	Loved that Diana thanks!!
    138 01:06:09	Gabby Palomo:	Readable code. I love that reason.
    139 01:07:26	Gabby Palomo:	My cognitive load or the computers?
    140 01:07:33	Diana Garcia Cortes:	Sorry, the phrase I was looking for was: avoid side effects.
    141 01:07:40	Diana Garcia Cortes:	https://medium.com/@alonr110/elevate-your-code-best-practices-for-function-writing-16d96481c0c7
    142 01:07:44	Gabby Palomo:	Reacted to "Sorry, the phrase I ..." with 🤯
    143 01:07:49	tataphani:	was that the Idea of DPLYR
    144 01:07:50	Gabby Palomo:	Reacted to "https://medium.com/@..." with 💯
    145 01:08:05	Steffi LaZerte (she/her):	One thing that is nice about for loops is that you can 'jump' in at any time to test things out. But if you learn the browser() function to put that in your map functions, that gives you the same power :D
    146 01:08:29	Gabby Palomo:	Replying to "One thing that is ni..."
    147 
    148 teach me your esoteric ways!!!!
    149 01:08:31	Steffi LaZerte (she/her):	Replying to "My cognitive load or..."
    150 
    151 I think yours :D
    152 01:08:47	Steffi LaZerte (she/her):	end
    153 01:08:56	Derek Sollberger (he/him):	Reacted to "One thing that is ni..." with 🧐
    154 01:09:34	Gabby Palomo:	Reacted to "One thing that is ni..." with 🧐
    155 01:10:22	Steffi LaZerte (she/her):	Replying to "One thing that is ni..."
    156 
    157 We'll get to it! https://adv-r.hadley.nz/debugging.html#browser
    158 01:10:36	Steffi LaZerte (she/her):	Replying to "One thing that is ni..."
    159 
    160 Another function that changed my life :)
    161 01:11:32	Gabby Palomo:	Replying to "One thing that is ni..."
    162 
    163 I am an old Padawan I have much to learn
    164 01:11:50	Gabby Palomo:	Reacted to "We'll get to it! htt..." with 💯
    165 01:12:12	Steffi LaZerte (she/her):	Replying to "One thing that is ni..."
    166 
    167 It never stops!
    168 01:12:20	Gabby Palomo:	Reacted to "It never stops!" with ❤️
    169 01:12:23	Derek Sollberger (he/him):	Thank you for this great example of `glue`
    170 01:12:35	Gabby Palomo:	Reacted to "Thank you for this g..." with 💯
    171 01:12:37	Diana Garcia Cortes:	Ok like python f-strings
    172 01:13:27	Diana Garcia Cortes:	Thanks! I didn’t know the glue package
    173 01:14:23	Leo:	many thanks
    174 01:15:54	tataphani:	thanks
    175 ```
    176 </details>