Programming Review

Working Environment and Programming Logic

Stat 251

2025-01-30

Last Class

  • Worked on Homework #2

  • Identified several useful functions and packages:

    • packages/libraries

      • in R: stringr, stringi
      • in python: re
    • functions

      • in R: tolower, stri_reverse, stri_sub_all
      • in python: .lower(), [::-1], re.sub
  • If you made at least one submission to github, you have some points for homework #2 in Canvas

Today’s goal

  • make at least one submission to Github

  • Review function writing: section 13

  • Code chunks in quarto documents

What Chatgbt thinks palindromes are …

Create some palindromes with topics from the R tidyverse

ChatGPT said:

Sure, I can get creative with that! Here are some palindromes inspired by the R tidyverse package:

ggplot palindrome:

“plot, ggplot, ggplot, top, lg”

dplyr palindrome:

“pipe, dep, ed, epip”

tidyr palindrome:

“tidy, rid, id, ydit”

stringr palindrome:

“strings, r, r, sgnirts” tibble palindrome:

The first one is not a palindrome

ChatGPT said:

You’re absolutely right! My bad on that one. A palindrome needs to read the same backward and forward. Let me fix that for you! Here’s a proper one:

For ggplot:

“ggplot, tol, log, gg”

That works better as a true palindrome. Thanks for catching that! Would you like more examples, or should I tweak any of the others?

From a block of code to functions

When you execute the same code multiple times, you should wrap the code into a function:

Code block to function

Function definitions

As an extra challenge … (for extra credit)

Can you establish how fast your function palindrome is?

Measuring speed in R

Measuring speed in python

Which is faster? R or python? - You might have to make multiple calls to the functions to capture differences

Dos and Don’ts

There are some things we should not (ever) do in R Markdown (or quarto) documents:

  • Install a package

Next Time

  • Review

    • indexing,
    • data frames/structures,
    • thinking in column-shaped objects
  • Coming up: Self Assessment quiz